Compare commits
No commits in common. "e5b35f3fcc75e6d89551505738628997e890f44b" and "0f903cc8ec8936c34c2325bf95750d4883eb7911" have entirely different histories.
e5b35f3fcc
...
0f903cc8ec
@ -1,31 +1,31 @@
|
|||||||
package ucontainer
|
package ucontainer
|
||||||
|
|
||||||
// Optional can either hold a value, or nothing.
|
// Optional is an optional value.
|
||||||
type Optional[T any] struct {
|
type Optional[T any] struct {
|
||||||
value T
|
value T
|
||||||
exists bool
|
exists bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// O creates a new optional with the specified value.
|
// Value returns the value and true if the value exists. If not, it returns the
|
||||||
func O[T any] (value T) Optional[T] {
|
// last set value and false.
|
||||||
return Optional[T] {
|
func (this *Optional[T]) Value () (T, bool) {
|
||||||
value: value,
|
return this.value, this.exists
|
||||||
exists: true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Void returns an optional with no value.
|
// Set sets the value.
|
||||||
func Void[T any] () Optional[T] {
|
func (this *Optional[T]) Set (value T) {
|
||||||
return Optional[T] { }
|
this.value = value
|
||||||
|
this.exists = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unset unsets the value.
|
||||||
|
func (this *Optional[T]) Unset () {
|
||||||
|
var zero T
|
||||||
|
this.value = zero
|
||||||
|
this.exists = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exists returns if the value is currently set.
|
// Exists returns if the value is currently set.
|
||||||
func (optional Optional[T]) Exists () bool {
|
func (this *Optional[T]) Exists () bool {
|
||||||
return optional.exists
|
return this.exists
|
||||||
}
|
|
||||||
|
|
||||||
// Value returns the value and true if the value exists. If not, it returns the
|
|
||||||
// zero value and false.
|
|
||||||
func (optional Optional[T]) Value () (T, bool) {
|
|
||||||
return optional.value, optional.exists
|
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,6 @@ package ucontainer
|
|||||||
// Set is a set of unique items, built on top of map.
|
// Set is a set of unique items, built on top of map.
|
||||||
type Set[T comparable] map[T] struct { }
|
type Set[T comparable] map[T] struct { }
|
||||||
|
|
||||||
// NewSet creates a new set that contains all specified items.
|
|
||||||
func NewSet[T comparable] (items ...T) Set[T] {
|
|
||||||
set := make(Set[T])
|
|
||||||
for _, item := range items {
|
|
||||||
set.Add(item)
|
|
||||||
}
|
|
||||||
return set
|
|
||||||
}
|
|
||||||
|
|
||||||
// Empty returns true if there are no items in the set.
|
// Empty returns true if there are no items in the set.
|
||||||
func (set Set[T]) Empty () bool {
|
func (set Set[T]) Empty () bool {
|
||||||
return set == nil || len(set) == 0
|
return set == nil || len(set) == 0
|
||||||
|
Reference in New Issue
Block a user