`filter` is a bad name

Whenever I want to filter an iterator, I always reach for filter. However, even though I come from the land of haskell, I always forget which way the predicate lies. Does it keep elements for which the predicate returns true, or false?

I propose this function be split up into 2: keep_if and skip_if.

Thoughts?

+1. keep_if and skip_if are more discoverable to users less familiar with Haskell too.

Filter is a classic name for this function. I wouldn’t mind some aliases, but I’d never use them.

3 Likes

I think filter is fine name. Its has many precedent in other functional language like haskell and scala.

1 Like

I agree with reasoning behind this “should it be true or false”. But I don’t think “skip_if” is a great name in a presense of “skip_while”, though just “keep_if” sounds good.

According to http://en.wikipedia.org/wiki/Filter_(higher-order_function) filter name is used also in JavaScript, Java, Python, Swift, Scala, Erlang, Kotlin, Closure, D, Scheme, Prolog, OCaml.

Second most used name is select in Mathematica, Ruby and Smalltalk.

C#/LINQ uses the SQL-inspired “Where”, which reads completely unambiguously.

Where is already a language keyword (used for more generalized bounds on types) and I wouldn’t favor trading its current use for an arguably better method name on iterators.

I’m comfortable with filter; this is just FYI…

Ruby’s select is also known as find_all: http://ruby-doc.org/core-2.1.3/Enumerable.html#method-i-select

There are also reject and partition.

[1] pry(main)> [1,2,3].select{|i| i%2 == 0}
=> [2]
[2] pry(main)> [1,2,3].reject{|i| i%2 == 0}
=> [1, 3]
[3] pry(main)> [1,2,3].partition{|i| i%2 == 0}
=> [[2], [1, 3]]
1 Like

I don’t mean to imply that my names are the only one. Only that filter does not give any indication as to the direction of the boolean, and a name that does should be used instead. select/reject would be fine, as would where, etc.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.