Last active
November 30, 2017 22:05
-
-
Save hughfdjackson/6062159 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // is maybe a functor if it doesn't try-catch? | |
| // Functor law (in haskell): | |
| fmap id = id | |
| fmap (p . q) = (fmap p) . (fmap q) | |
| // in js: | |
| func.map(_.identity) === _.identity(func) | |
| func.map(_.compose(a, b)) === func.map(b).map(a) | |
| // consider this, though: | |
| var unpackX = function(o){ return o.x } | |
| var unpackY = function(o){ return o.y } | |
| var unpackXY = _.compose(unpackY, unpackX) | |
| unpackXY({ x: { y: 3 } }) //= 3 | |
| unpackXY('foo') //= error: undefined is not an object | |
| // Maybe maps over an object unless it holds `null` or `undefined` - the js 'nothing' values. | |
| // in this case, there is no try-catch gaurds in map. | |
| Maybe('foo').map(unpackXY) //= error - undefined is not an object | |
| Maybe('foo').map(unpackX).map(unpackY) //= no error - undefined causes a skip. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe is an abstract supertype of the types
JustandNothingand as such maybe is not a functor.Both
JustandNothingare functorsThe way you normally use
Maybeis with a method likefromNullablewhich returns either aJustorNothingmeaning you need to
chainnotmapover it in order to do these multiple step procedures.