Skip to content

Instantly share code, notes, and snippets.

Thomas Collardeau collardeau

Block or report user

Report or block collardeau

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View box.js
class Box {
constructor(x) {
this.value = x;
}
static of(x) {
return new Box(x);
}
log() {
console.log(this.value);
}
@collardeau
collardeau / script.js
Last active Nov 10, 2015
Intro To FP Concepts, Part 3
View script.js
// functor factory
const right = {
of: val => functor(right, val),
fmap: function(f){
return right.of(f(this.val));
}
}
const left = {
@collardeau
collardeau / script.js
Last active Nov 6, 2015
Intro To FP Concepts, part2
View script.js
const Maybe = val => {
return {
val: val,
fmap: function(fn) {
if(this.val === null) return Maybe(null);
return Maybe(fn(this.val));
}
}
};
View script.js
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]);
View functional-js-02b.js
// Standalone ES6 composition mentions "person" twice
const getFirstInitial = person => getFirstLetter(getFirstName(person))
// vs. Ramda's pipe composition
const firstInitial = pipe(getFirstName, getFirstLetter);
@collardeau
collardeau / func.js
Last active Oct 23, 2015
func-js-01
View func.js
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"
View functional-js-06.js
const getFirstName = maybeName => maybeName.fmap(name => name.split(" ")[1]);
const getFirstLetter = maybeString => maybeString.fmap(string => string[0]);
View functional-js-05.js
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");
View functional-js-04.js
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];
}
View functional-js-02.js
const pipe = require("ramda").pipe; // node or webpack/browserify
const firstInitial = pipe(getFirstName, getFirstLetter);
You can’t perform that action at this time.