Last active
September 27, 2018 13:39
-
-
Save djleonskennedy/5197342a51fc653d6c7cf6d68daa6df5 to your computer and use it in GitHub Desktop.
some FP stuff :)
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
// compose | |
const compose = (...fns) => x => | |
fns.reduceRight((v, f) => f(v), x); | |
/* | |
const toSlug = compose( | |
encodeURIComponent, | |
join('-'), | |
map(toLowerCase), | |
split(' ') | |
); | |
console.log(toSlug('JS Cheerleader')); // 'js-cheerleader */ | |
//curry | |
export const curry = fn => (...args) => | |
args.length < fn.length ? curry(fn.bind(null, ...args)) : fn(...args) | |
//pipe | |
const pipe = (...fns) => x => | |
fns.reduce((v, f) => f(v), x); | |
/* | |
const fn1 = s => s.toLowerCase(); | |
const fn2 = s => s.split('').reverse().join(''); | |
const fn3 = s => s + '!' | |
const newFunc = pipe(fn1, fn2, fn3); | |
*/ | |
const result = newFunc('Time'); // emit! | |
//trace | |
const trace = curry((label, x) => { | |
console.log(`== ${ label }: ${ x }`); | |
return x; | |
}); | |
/* | |
const toSlug = pipe( | |
trace('input'), | |
split(' '), | |
map(toLowerCase), | |
trace('after map'), | |
join('-'), | |
encodeURIComponent | |
); | |
console.log(toSlug('JS Cheerleader')); | |
// '== input: JS Cheerleader' | |
// '== after map: js,cheerleader' | |
// 'js-cheerleader' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment