`filter` is a bad name

#1

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?

0 Likes

#2

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

0 Likes

#3

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

3 Likes

#4

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

1 Like

#5

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.

0 Likes

#6

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.

0 Likes

#7

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

0 Likes

#8

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.

0 Likes

#9

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

#10

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.

0 Likes

closed #11

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

0 Likes