JuliaLang/julia — pull request #9340 by rfourquet (2014-12-13 12:42:58+00:00)
Title: WIP/RFC: make find, filter, count more consistent

This is about adding the method `count(A, value)`, as `count(x -> x==value, A)` is frustratingly verbose and slow. Then it is tempting to homogeneize a group of functions which can have those three methods:

1. f(pred, A) -> pred is applied to all A elements
2. f(A, val) <==> f(x -> x == val)
3. f(A) <==> f(x -> x != 0)

`findnext` and `findfirst` already have them, `count` had only 1. (and 3. via `countnz`), `find` had 1. and 3.

* I started applying the same for `any` and `all`, but then it is conflicting with `any(A, dims)` taking dimensions. This seems OK to leave it as it is (at least for `any(A, value)` <==> `value in A`).
* if `A` is a `Dict`, I chose to interpret `filter(A, value)` as comparing `value` to the values of `A`, not its keys. But then `Regex` makes the opposite choice, which I find surprising: `filter!(r::Regex, d::Dict) = filter!((k,v)->ismatch(r,k),d)`. WDYT?
* `findn` and `findnz` would be happy to have those methods added too, but `findnz` looks like it finds non-zeros, so it should be renamed in this case. What does the `n` in `findn` stand for?

If this makes sense, I will add some tests before removing WIP.