-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package opt | ||
|
||
// None represents the absence of a value. | ||
type None[V any] struct { | ||
} | ||
|
||
// Assert that None[V] implements Optional[V] | ||
var _ Optional[any] = None[any]{} | ||
|
||
func (n None[V]) Present() bool { | ||
return false | ||
} | ||
|
||
func (n None[V]) Get() (V, bool) { | ||
var zero V | ||
return zero, false | ||
} | ||
|
||
func (n None[V]) GetOrZero() V { | ||
var zero V | ||
return zero | ||
} | ||
|
||
func (n None[V]) GetOrDefault(defaultValue V) V { | ||
return defaultValue | ||
} | ||
|
||
func (n None[V]) GetOrFunc(f func() V) V { | ||
return f() | ||
} | ||
|
||
func (n None[V]) Filter(_ func(V) bool) Optional[V] { | ||
return n | ||
} | ||
|
||
func (n None[V]) IfPresent(_ func(V)) bool { | ||
return false | ||
} | ||
|
||
func (n None[V]) IfPresentElse(_ func(V), f func()) bool { | ||
f() | ||
return false | ||
} | ||
|
||
func (n None[V]) String() string { | ||
return "None" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package opt | ||
|
||
import "fmt" | ||
|
||
// Some represents the presence of a value. | ||
type Some[V any] struct { | ||
Value V | ||
} | ||
|
||
// Assert that Some[V] implements Optional[V] | ||
var _ Optional[any] = Some[any]{} | ||
|
||
func (s Some[V]) Present() bool { | ||
return true | ||
} | ||
|
||
func (s Some[V]) Get() (V, bool) { | ||
return s.Value, true | ||
} | ||
|
||
func (s Some[V]) GetOrZero() V { | ||
return s.Value | ||
} | ||
|
||
func (s Some[V]) GetOrDefault(_ V) V { | ||
return s.Value | ||
} | ||
|
||
func (s Some[V]) GetOrFunc(_ func() V) V { | ||
return s.Value | ||
} | ||
|
||
func (s Some[V]) Filter(f func(V) bool) Optional[V] { | ||
if f(s.Value) { | ||
return s | ||
} | ||
return Empty[V]() | ||
} | ||
|
||
func (s Some[V]) IfPresent(f func(V)) bool { | ||
f(s.Value) | ||
return true | ||
} | ||
|
||
func (s Some[V]) IfPresentElse(f func(V), _ func()) bool { | ||
f(s.Value) | ||
return true | ||
} | ||
|
||
func (s Some[V]) String() string { | ||
return fmt.Sprintf("Some(%#v)", s.Value) | ||
} |