Skip to content

Instantly share code, notes, and snippets.

@naomik

naomik/improvements.js

Last active Aug 29, 2015
Embed
What would you like to do?
refactoring point-free improvements
// let pow = y => x => Math.pow(x, y);
// let pow = y => x => idx(y)(iterate(mult(x))(y)(1))
let pow = y => x => repeat(mult(x))(y)(1);
// let foldl = f => i => xs => xs.reduce((r, x) => f(r)(x), i);
let foldl = f => i => xs => xs.reduce(uncurry(f), i);
// let foldr = f => i => xs => xs.reduceRight((r, x) => f(r)(x), i);
let foldr = f => i => xs => xs.reduceRight(uncurry(f), i);
// let apply = f => xs => xs.reduce((f, x) => f(x), f);
// let apply = f => foldl(f => x => f(x))(f);
// let apply = foldl(f => x => f(x));
let apply = foldl(id);
// let compose = fs => x => fs.reduceRight((x, f) => f(x), x);
// let compose = fs => x => foldr(x => f => f(x))(x)(fs);
// let compose = flip(foldr(x => f => f(x)));
let compose = flip(foldr(flip(id)));
// how does flip(id) work?^
// cool!
// flip(id)(x)(f) -> id(f)(x)
// let repeat = f => i => x => idx(i)(iterate(f)(i)(x));
let repeat = f => i => compose([idx(i), iterate(f)(i)]);
let uncurry = f => (x, y) => f(x)(y);
// let partial = (f, x) => curry(f)(x);
// let partial = uncurry(f => x => curry(f)(x))
let partial = uncurry(curry);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.