Skip to content

Instantly share code, notes, and snippets.

@humanchimp
Created May 21, 2016 01:19
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 humanchimp/1e214794dbf43f93e232c6e92a506744 to your computer and use it in GitHub Desktop.
Save humanchimp/1e214794dbf43f93e232c6e92a506744 to your computer and use it in GitHub Desktop.
let flip = (flopped => {
return function flip(fn) {
if (flopped.has(fn)) {
return flopped.get(fn);
}
flopped.set(fn, flipped);
flopped.set(flipped, fn);
return flipped;
function flipped(...rest) {
return fn.apply(this, rest.reverse());
}
}
})(new WeakMap());
function partition(list, predicate) {
return list.reduce((memo, subject) => {
memo[1 - predicate(subject)].push(subject);
return memo;
}, [[],[]]);
}
let [ more, less ] = flip(partition)(it => it > 5, [0,1,2,3,4]);
assert(partition === flip(flip(partition)));
assert(flip(partition) === flip(flip(flip(partition))));
function assert(condition) { if (!condition) throw Error("assertion failed") }
undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment