Last active
November 6, 2015 14:27
-
-
Save collardeau/ea353f33e6eea28010d6 to your computer and use it in GitHub Desktop.
Intro To FP Concepts, part2
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
const Maybe = val => { | |
return { | |
val: val, | |
fmap: function(fn) { | |
if(this.val === null) return Maybe(null); | |
return Maybe(fn(this.val)); | |
} | |
} | |
}; | |
const map = R.curry((fn, functor) => functor.fmap(fn)); | |
// composing partially applied Ramda functions! | |
const firstInitial = R.pipe( | |
R.split(" "), | |
R.nth(1), | |
R.nth(0) | |
); | |
const getFirstInitial = map(firstInitial); | |
// let's try it out | |
const user = Maybe("Time-traveler Marty McFly"); | |
document.write(getFirstInitial(user).val); // "M" | |
const noUser = Maybe(null); | |
document.write(getFirstInitial(noUser).val) // null |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.18.0/ramda.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment