Created
May 21, 2016 01:19
-
-
Save humanchimp/1e214794dbf43f93e232c6e92a506744 to your computer and use it in GitHub Desktop.
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
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