Skip to content

Instantly share code, notes, and snippets.

@ebiggs
ebiggs / Maybe.coffee
Last active June 20, 2016 04:32
CoffeeScript Maybe Monad - couldn't find one with a Google search so I had to roll my own, hopefully this one showed up for yours ;) Good code coverage in the jasmine spec, so feel confident using it with reckless abandon ;) Maybe.coffee was definitely built with Some(coffea arabica) in my veins ;)
###
Like java, javascript is optimized for throwing null pointer exceptions
coffeescript helps with its series of ? operators
but here is a scala option-eqsue "maybe monad" when such semantics
are preferred over the ? operators or when you want your data
to self-document its indefinite-ness (regular vars don't warn you
that you may get null or undefined!).
note that we're perfectly happy to give you a Some(null) or a Some(undefined)
just like scala. if you have an x that you want to fmap to None if it's