Skip to content

Instantly share code, notes, and snippets.

@SirSerje
Last active February 11, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirSerje/827b50356de0f8c5f380adea5bc8bcb6 to your computer and use it in GitHub Desktop.
Save SirSerje/827b50356de0f8c5f380adea5bc8bcb6 to your computer and use it in GitHub Desktop.
Any kind of FP tweaks
const trace=label=>value=>{
console.log(`${ label }: ${ value }`);
return value;
};
const simpleTrace = value => console.log(value);
const pipe = (...fns) => (args) => fns.reduce((arg, fn) => fn(arg), args);
const compose = (...fns) => (args) => fns.reduce((arg, fn) => fn(arg), args);
const mult = a => b => a*b;
const add = (a) => (b) => a+b;
const double = a => a * 2;
const plusThree = a => a + 3;
compose (double, plusThree)(1); //8
compose(plusThree, double)(1)//5
compose (add(2), add(2))(1)//5
compose (plusThree, mult(3), add(1), double)(2)//18
pipe(double, add(1), mult(3), plusThree)(2)//18
pipe(double, trace('step2'), add(1), mult(3), simpleTrace, plusThree)(2)//18
const notFlattenArray = [1, [2], [[3]], [[[[4]]]]];
const flatt = (a, b=[]) => a.reduce((acc,val) => Array.isArray(val) ? flatt(val,acc) : [...acc, val] , b);
flatt(notFlattenArray); //[1,2,3,4]
//basic functer
class Container {
constructor(x) {
this.$value = x;
}
static of(x) {
return new Container(x);
}
}
Container.prototype.map = function (f) {
return Container.of(f(this.$value));
};
Container.of('flamethrowers').map(s => s.toUpperCase());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment