-
Notifications
You must be signed in to change notification settings - Fork 135
Maybe
johnmcclean-aol edited this page Nov 23, 2016
·
10 revisions
The Maybe type in cyclops-react is a totally lazy Option or Optional type modelled on Maybe from Haskell. Maybe's can represent one of two states - Just a value (present / some ) or None.
Maybe is an instance of the following cyclops types (and others) ApplicativeFunctor, Filterable, Foldable, Functor, MonadicValue1, To, Value,Visitable and Zippable
In cyclops we can use the visit operator to pattern match on the state the Maybe is in.
int result = Maybe.just(10)
.visit(some->some+5,()->-1);
Recover orElse and coflatMap can be used to manage Maybe's that aren't present and provide a default value.
Maybe.just(10)
.recover(-1); //Maybe[10]
Maybe.none()
.recover(-1); //Maybe[-1]
int result = Maybe.none()
.orElse(-1); //-1
Maybe.none()
.coflatMap(maybe->maybe.visit(Maybe::get,()->-1); //Maybe[-1]
oops - my bad