DEV Community

Discussion on: Functional Programming in JavaScript? Yes, Please.

Collapse
 
bcallaars profile image
Joseph Callaars • Edited

I like this article, it's very clear. Well done! The only comment I have are about the semantics around filter. To "filter" means to "remove", so I would think (or presume) that a filter is an exclusive filter and not an inclusive filter like you use it.

I know this all comes down to semantics, so I'm sure it's a matter of opinion, but when I read filter(isCat) I feel that it shouldn't contain cats.

What is your opinion about this?

Collapse
 
paulraines68 profile image
Paul Raines

Some dictionaries use "remove" in their definition but most define the verb filter as simply passing something through a filter (noun). As a verb, filter is most often followed by either "for" or "out" to make it clear. So even in the 1800's I think a gold miner would say he is using his mesh pan "to filter for gold".

Collapse
 
bcallaars profile image
Joseph Callaars

I accept your explanation, and I fully understand it, albeit I don't agree. When working with computers, a filter means to take impurities out, e.g. a spam filter. So I do understand that this is due to dynamics of a language, but if it was in my code I'd be confused if I saw it used this way.

But again, thanks for your comment :).

Thread Thread
 
zerdnelemo profile image
zerdnelemo

I think that the meaning of "filter" remains the same. And the concept we can apply in programming is filter removes the "impurities", keeping only the desirable data by passing the a parameter (the desirable condition).

Collapse
 
jamsesso profile image
Sam Jesso

I hear you, Joseph. I always had to stop and think about filter until I started thinking of it as a "WHERE" clause in SQL.

When I see filter(isCat) I immediately think of it as WHERE isCat.

Collapse
 
moopet profile image
Ben Sinclair • Edited

This bothers me too.
The callback must return true to include the element in the resultant array, and if we were using filter to remove cats, then we'd need isCat to return false if the element was a cat or to rename it to isNotCat and make it keep returning true. That means either way we can't use that function in both filter and our own code without going insane.

Oh, and in response to your next comment (which for some reason dev.to doesn't let me reply to directly): we have an existing convention of referring to filters on search forms but what they include. If you go to Amazon and search for something, the filters aren't all like "do not include fish food in my results" because that would also be unsuitable for happy life.