containers icon indicating copy to clipboard operation
containers copied to clipboard

Add `filterKeys`

Open emilypi opened this issue 3 years ago • 6 comments

Hi All,

A more general form of restrictKeys would be filterKeys, implemented thusly:

filterKeys :: (k -> Bool) -> Map k a -> Map k a 
filterKeys p = filterWithKey (const . p)

Which would allow restrictKeys to be implemented simply as

restrictKeys :: Set k -> Map k a -> Map k a 
restrictKeys s = filterKeys (`Set.member` s)

Is there an appetite for this kind of change? filterKeys is a pretty generally useful function, and restrictKeys tends to be a little too specialized too suit my needs. Since the change would arguably improve the existing functions, I think it's worth a thought.

emilypi avatar Nov 03 '22 01:11 emilypi

restrictKeys exists because it can be particularly efficient.

treeowl avatar Nov 03 '22 01:11 treeowl

alright, but what about the addition of filterKeys then? (assuming we leave restrictKeys alone)

emilypi avatar Nov 03 '22 02:11 emilypi

I don't feel strongly about it. I guess it adds symmetry, balancing filter.

treeowl avatar Nov 03 '22 02:11 treeowl

I'll go do the needful then. Additionally, I'm finding somethings are used internally, with full haddocks that would be useful to export, but are not (e.g. filterWithKeyA). Can we export those as well?

emilypi avatar Nov 03 '22 02:11 emilypi

filterWithKeyA would be a good one, yeah. Maybe some more mergeA machinery too.

treeowl avatar Nov 03 '22 02:11 treeowl

I'll raise separate PRs to not dirty the history

emilypi avatar Nov 03 '22 03:11 emilypi