Skip to content

Instantly share code, notes, and snippets.

@collardeau
Last active November 6, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collardeau/ea353f33e6eea28010d6 to your computer and use it in GitHub Desktop.
Save collardeau/ea353f33e6eea28010d6 to your computer and use it in GitHub Desktop.
Intro To FP Concepts, part2
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
<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