-
-
Save anonymous/b94c96cbb78fe9966e93964d7cb34e9e 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
const unionBy = p => xs => ys => { | |
// this scope has knowledge that a Set is used | |
// don't leak knowledge of Set leak into user-supplied predicate | |
let s = createSet(xs); | |
// this line is left expanded for clarity | |
// let zs = filter (y => p (z => s.has(z) ? false : s.add(z)) (y)) (ys) | |
// it eta reduces to | |
let zs = filter (p (z => s.has(z) ? false : s.add(z))) (ys); | |
return concat(xs) (afrom (zs)); | |
}; | |
// then union and unionCI change like this | |
// see? no knowledge that a Set was used ^_^ | |
const union = unionBy (apply); | |
const unionCI = unionBy (p=> x=> p(x.toLowerCase())); | |
// you will probably want this then too | |
const unionByAll = p => (xs,...xxs) => { | |
return foldl (unionBy (p)) (xs) (xxs); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I fully grasped your solution finally. The magic is in the interaction between
unionBy
andp
. I love it.