Last active
October 24, 2015 10:21
-
-
Save collardeau/96c65324fade6d21577e to your computer and use it in GitHub Desktop.
JYpLEY
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 => ({ | |
val, | |
fmap(f) { | |
if(this.val === null || this.val === undefined) return Maybe(null); | |
return Maybe(f(this.val)); | |
} | |
}); | |
const getFirstName = maybeName => maybeName.fmap(name => name.split(" ")[1]); | |
const getFirstLetter = maybeString => maybeString.fmap(string => string[0]); | |
const firstInitial = R.pipe(getFirstName, getFirstLetter); | |
// let's try this out | |
const user = Maybe("Bully Biff Tannen"); | |
const initial = firstInitial(user); | |
document.write(initial.val, "<br />"); // "B" | |
const noUser = Maybe(null); | |
const noInitial = firstInitial(noUser); | |
document.write(noInitial.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