diff --git a/map.go b/map.go index 5717547..6f93c08 100644 --- a/map.go +++ b/map.go @@ -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]() } diff --git a/optional.go b/optional.go index e46dafc..1638d4c 100644 --- a/optional.go +++ b/optional.go @@ -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() } } diff --git a/then.go b/then.go index 2b39d0c..7ab54b1 100644 --- a/then.go +++ b/then.go @@ -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]() }