This file contains 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 pipe = require("ramda").pipe; // node or webpack/browserify | |
const firstInitial = pipe(getFirstName, getFirstLetter); |
This file contains 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 getFirstName = name => { | |
if(name === null) return null; // new | |
return name.split(" ")[1]; | |
} | |
const getFirstLetter = word => { | |
if(word === null) return null; // new | |
return word[0]; | |
} |
This file contains 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) return Maybe(null); | |
return Maybe(f(this.val)); | |
} | |
}); | |
// lets create some containers with our Maybe factory | |
let user = Maybe("Slacker George McFly"); |
This file contains 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 getFirstName = maybeName => maybeName.fmap(name => name.split(" ")[1]); | |
const getFirstLetter = maybeString => maybeString.fmap(string => string[0]); | |
This file contains 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 getFirstName = person => person.split(" ")[1]; // second word | |
const getFirstLetter = string => string[0]; // first letter | |
const getFirstInitial = person => { | |
return getFirstLetter(getFirstName(person)); // nested function! | |
} | |
// try it | |
getFirstInitial("Doc Emmett Brown"); // "E" |
This file contains 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
// Standalone ES6 composition mentions "person" twice | |
const getFirstInitial = person => getFirstLetter(getFirstName(person)) | |
// vs. Ramda's pipe composition | |
const firstInitial = pipe(getFirstName, getFirstLetter); |
This file contains 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]); |
This file contains 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)); | |
} | |
} | |
}; |
This file contains 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
// functor factory | |
const right = { | |
of: val => functor(right, val), | |
fmap: function(f){ | |
return right.of(f(this.val)); | |
} | |
} | |
const left = { |
This file contains 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
class Box { | |
constructor(x) { | |
this.value = x; | |
} | |
static of(x) { | |
return new Box(x); | |
} | |
log() { | |
console.log(this.value); | |
} |
OlderNewer