Existence
Fantasy Land Compliant!
Indigenous Complaint!
A monadic wrapper around values that may or may not exist. Inspired by
Scala's Option
monad. Unlike Scala's implementation, we have a really
small interface and we purposefully don't implement Existence#get()
.
The problem with Existence#get()
is it lets you side step the contextual
bounds by unsafely detaching the value from the context. This results in
the same NPE try/catch soup that Option
is intended to avoid.
Examples
Monoid concatenation example:
var Some = ;var None = ; var list = 1 2 3 4 5 6;var added = list; console;// prints "value: [1, 2, 3, 4, 5, 6]"
Functor composition example:
var Some = ;var None = ; var some = 21;var answer = some;console;// prints "value: 42" var none = ;var nada = none;console;// prints "exists = false"
Applicative composition example:
var Existence = ;var Some = ;var None = ;var curry = curry; var someNumber = 42;var someString = "beep";var someBoolean = true;var none = ; var lifted = Existence; var allThere = lifted;console;// prints "value: 42 - beep - true" var missing = lifted;console;// prints "value: None"
Monadic composition (flatMap / chain) example (flatMap is an alias for chain):
var Some = ;var None = ; var value = 42;var filtered = value; console;// prints "value: 42"