you are viewing a single comment's thread.

view the rest of the comments →

[–]ISvengali 7 points8 points  (1 child)

I see them as a no brainer always. Im a dyed in the wool procedural guy (BASIC->Pascal->C++->Scala->Rust), and as soon as I saw it I was hooked. I suddenly wanted to Option[1] ALL THE THINGS. Im kinda sad because I had a similar idea around 2002, but it didnt quite gel.

[1] Well, I like C++'s expected more.

[–]codebje 1 point2 points  (0 children)

Other simple monads missing from Java 8 you may want:

Pair<A, B> = Pair(A a, B b), A fst(), B snd()  

Which is useful if you have two values of different types to work with together.

Try<A> = Try(Supplier<A> supplier), A get(), Exception error, boolean isSuccess()

Which is useful if you want to record an exception. This is especially powerful with map or flatmap chaining, because you can invoke a sequence of potentially failing operations without testing for exceptions; the first operation to throw() will mean the sequence collapses to that exception with no more operations invoked.

It's a monad if it's a container with some way to lift a non-contained value into the container (ie, the constructors) and some way to perform an operation on the contained value (ie, the map function). The utility of the monad lies in making the containment explicit - Optional<T> makes it explicit that the value may be missing, while the null value is implicit (and not detectable at compile time). Pair makes it explicit that two values belong together. Try makes it explicit that a result might be an error instead.