Monads/Maybe monad: Difference between revisions

Content added Content deleted
m (Added language identifier and moved a comment.)
Line 992: Line 992:


=={{header|Java}}==
=={{header|Java}}==
Java has a built-in generic "Maybe" monad in form of the Optional<T> class.


The class has static methods, "of" and "ofNullable", which act as the unit function
<syntaxhighlight>
for wrapping nullable and non-nullable values respectively.

The class instance method, "flatMap", acts as the bind function.
<syntaxhighlight lang="java">
import java.util.Optional;
import java.util.Optional;


/**
* Java has a built-in generic "Maybe" monad in form of the Optional<T> class.
*
* The class has static methods, "of" and "ofNullable", which act as the unit function
* for wrapping nullable and non-nullable values respectively.
* The class instance method, "flatMap", acts as the bind function. *
*/
public final class MonadMaybe {
public final class MonadMaybe {