.isnothing()

master
Charles Iliya Krempeaux 2023-09-24 05:24:56 +09:00
parent 58bf264aa9
commit b434dd95d3
3 changed files with 9 additions and 5 deletions

2
map.go
View File

@ -39,7 +39,7 @@ package opt
//
// // result2 == opt.Nothing[int]()
func Map[T1 any, T2 any](op Optional[T1], fn func(T1)T2) Optional[T2] {
if !op.something {
if op.isnothing() {
return Nothing[T2]()
}

View File

@ -107,7 +107,7 @@ func Something[T any](value T) Optional[T] {
//
// op3 = op3.Filter(fn)
func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
if !receiver.something {
if receiver.isnothing() {
return Nothing[T]()
}
@ -118,6 +118,10 @@ func (receiver Optional[T]) Filter(fn func(T)bool) Optional[T] {
return receiver
}
func (receiver Optional[T]) isnothing() bool {
return !receiver.something
}
// Get returns the value inside of the optional-type if it is holding something.
//
// Example usage:
@ -162,7 +166,7 @@ func (receiver Optional[T]) Get() (T, bool) {
// // Output:
// // op = opt.Nothing[uint8]()
func (receiver Optional[T]) GoString() string {
if !receiver.something {
if receiver.isnothing() {
return fmt.Sprintf("opt.Nothing[%T]()", receiver.value)
}
@ -183,7 +187,7 @@ func (receiver Optional[T]) GoString() string {
// //@TODO
// })
func (receiver Optional[T]) WhenNothing(fn func()) {
if !receiver.something {
if receiver.isnothing() {
fn()
}
}

View File

@ -36,7 +36,7 @@ package opt
//
// // result2 == opt.Nothing[byte]()
func Then[T1 any, T2 any](op Optional[T1], fn func(T1)Optional[T2]) Optional[T2] {
if !op.something {
if op.isnothing() {
return Nothing[T2]()
}