Closed my-little-repository closed 8 years ago
length
can't be defined on a lazy filter, without evaluating the whole filter (or a theorem prover I suppose). There has been some discussion of making the default filter eager, but for now, use one of length(collect(filter(i -> i>3, i for i = 1:5)))
or count(i->i>3, i for i = 1:5)
(if you're only interested in the count, the latter is better).
Yes, this is intentional. length
is not too useful if it requires running through the whole iterator, which is why we also have count
.
We could add a 1-argument method to count
, but I fear it would be unclear whether it counts all elements, or elements equal to true
(more like the existing 2-argument method).
In Python, it is idiomatic to do sum(1 for x in xs)
. This should work in Julia too, at least on nightly since #18695 was merged.
There seems to be a missing method for length when used on a filtered generator.
This is loosely connected to #16884 which reports a missing method for
findfirst
.Note that
count(x -> true, filter(i-> i>2, (i for i = 1:5)))
works but it is a bit weird to compute the length that way.