Skip to content

Instantly share code, notes, and snippets.

@collardeau
Last active October 24, 2015 10:21
Show Gist options
  • Save collardeau/96c65324fade6d21577e to your computer and use it in GitHub Desktop.
Save collardeau/96c65324fade6d21577e to your computer and use it in GitHub Desktop.
JYpLEY
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
<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