Closed orbitoo closed 2 years ago
package main import "github.com/samber/mo" func main() { var x float64 = 2.0 op := mo.Some(x) square := func(x float64) mo.Option[float64] { return mo.Some(x * x) } cubic := func(x float64) mo.Option[float64] { return mo.Some(x * x * x) } print(op.FlatMap(square).FlatMap(cubic).OrElse(-1)) }
This is an example of using this package, but I suppose function square should be
square := func(x float64) float64 { return x * x }
This declaration of mapping functions is better, I think.
Consider change the FlatMap into
func (o Option[T]) FlatMap(mapper func(T) T) Option[T] { if o.isPresent { return Some(mapper(o.value)) } return None[T]() }
That is what o.Map do
o.Map
FlatMap can return a None value.
FlatMap
None
Map always returns a Some value.
Map
Some
I don't see any error here 🤔.
I'm closing. Feel free to open it again for more questions!
This is an example of using this package, but I suppose function square should be
This declaration of mapping functions is better, I think.
Consider change the FlatMap into