Skip to content

Instantly share code, notes, and snippets.

/unionBy.js Secret

Created Sep 8, 2016
Embed
What would you like to do?
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);
};
@ivenmarquardt

This comment has been minimized.

Copy link

ivenmarquardt commented Sep 8, 2016

The original function was concat_(xs) (filter(notf(has(zs))) (ys)), which I converted into concat_(xs) (filter(pred(zs)) (ys)), which was a bad idea. To separate it in two lines is good regarding readability. I'll edit SO tomorrow. And I really forgot the let zs declaration - brain damage. Thanks Naomi.

@ivenmarquardt

This comment has been minimized.

Copy link

ivenmarquardt commented Sep 9, 2016

I fully grasped your solution finally. The magic is in the interaction between unionBy and p. I love it.

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.