Skip to content

Instantly share code, notes, and snippets.

View collardeau's full-sized avatar
🏠
Working from home

Thomas Collardeau collardeau

🏠
Working from home
View GitHub Profile
const pipe = require("ramda").pipe; // node or webpack/browserify
const firstInitial = pipe(getFirstName, getFirstLetter);
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];
}
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");
const getFirstName = maybeName => maybeName.fmap(name => name.split(" ")[1]);
const getFirstLetter = maybeString => maybeString.fmap(string => string[0]);
@collardeau
collardeau / func.js
Last active October 23, 2015 19:20
func-js-01
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"
// Standalone ES6 composition mentions "person" twice
const getFirstInitial = person => getFirstLetter(getFirstName(person))
// vs. Ramda's pipe composition
const firstInitial = pipe(getFirstName, getFirstLetter);
@collardeau
collardeau / script.js
Last active October 24, 2015 10:21
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]);
@collardeau
collardeau / script.js
Last active November 6, 2015 14:27
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));
}
}
};
@collardeau
collardeau / script.js
Last active November 10, 2015 11:25
Intro To FP Concepts, Part 3
// functor factory
const right = {
of: val => functor(right, val),
fmap: function(f){
return right.of(f(this.val));
}
}
const left = {
class Box {
constructor(x) {
this.value = x;
}
static of(x) {
return new Box(x);
}
log() {
console.log(this.value);
}