logoalt Hacker News

jiehongyesterday at 9:44 PM10 repliesview on HN

> … to call them Select and Where.

While map is a great name, I always struggle to remember if ‘filter’ keeps elements that match the condition or removes them.

I mean, it’s like a colander: you filter noodles and water, but which one do you keep? The noodles, right? But, replace noodles with tea and now you want to keep the water part.

Naming is hard I guess.


Replies

saghmtoday at 2:15 AM

There's always the Ruby strategy of just making all the names work. `select` and `filter` are buddies and you can use whichever you want or even go back and forth. Not a fan of `reduce`? That's fine, `inject` has got your back. Miss getting to type `collect` from Java or Rust? Don't worry, just use it instead of `map`, it's the same thing.

reddit_cloneyesterday at 11:08 PM

Talking about un-guessable, misleading function names,

C++ std::remove.

I would never have guessed what it does exactly. (It moves elements that match the filter to the front, and moves the end-marker forward. Leaves all the elements in the collection. You need to erase them yourself. )

show 2 replies
seanw444yesterday at 10:05 PM

I've never run into a generic "filter" function which keeps only the non-matching elements.

show 2 replies
rmunntoday at 2:45 AM

The filter keeps the tea... it's just that you then lift the filter out of the cup, carrying the tea with it. Flip your brain around to see it from that direction and it might help you with the mnemonics.

andrekandreyesterday at 11:13 PM

  > I always struggle to remember if ‘filter’ keeps elements that match the condition or removes them
if you had parameter names maybe it might help?

`filter(where:)` like in swift...?

show 1 reply
pavlovyesterday at 10:07 PM

Maybe those two could be filter_for (the “where” case) and filter_out.

show 1 reply
dcminteryesterday at 10:15 PM

If you're making tea with a colander something is very wrong ;)

show 2 replies
thaumasiotestoday at 7:47 AM

> While map is a great name, I always struggle to remember if ‘filter’ keeps elements that match the condition or removes them.

In Common Lisp both functions exist, under the names `remove-if` and `remove-if-not`.

quaverquavertoday at 12:00 AM

select/reject (Ruby)