I’m searching for a way to get the lenght from a iterator. The iterator can be consumed, is not problem. So, I’m find Query.jl, IterTools.jl and the iterator methods from base, but i’m not finding something to calculate the length from a iterator.
is not a fast operation, but you can define your own counter:
function mylength(iter)
n=0
for i in iter
n+=1
end
return n
end
the arguments in the github are valid in my opinion, what happens if an iterator has a side effect? what if it is a non-terminating one?. if an custom iterator type has a length, is necessary to define it, in my opinion
Yeah, but in my experience counting elements from a iterator (consuming it) is a very common operation and if that has side effects its something normal. I suppose it is a habit.
function was named ilen, not len since dispatching on itr is not well type-defined.
may be have a look to count. IterTools.jl package too
o(n) complexity expected.
I am proponent to move Iterators in stdlib adding some missing funcs like takewhile, dropwhile, compress, imap, ilength from IterTools
var list= new[] {1,2,3,4,5}
return list.Where(it=>it%2==0).Count()
I imagine in Julia it’s easy to do this type of calculations in streamming, without creating DataFrames, when i’m doing explorations over the data.
PD: It’s only a example, the real thing its much more complicated and with very large CSV’s, so, DataFrames is not a option. And i can cache in a dataframe a small set of data to do the develop loop fast, but in production i want to stream the data.
Btw do you still need a function to calculate the length of an iterator by consuming it (if necessary)? And if yes, what would you like it to return if it is known to have infinite length?
According to Interfaces · The Julia Language you can try to query whether the iterator itr has a length or not with Base.IteratorSize(itr). If this returns a Base.HasLength or a Base.HasShape, then the itr should provide a length method for querying the number of items.
Though I have to admit that this may not be reliable:
Base.IteratorSize is marked as optional. Moreover, there’s a fallback method that returns Base.HasLength() even though there may not be a length method. Example: struct A; end; a=A(); Base.IteratorSize(a) returns Base.HasLength() but length(a) errors (of course).
These methods need to be qualified with Base. as they are not exported, hinting that they may not be considered official interfaces, although they are documented in the interfaces section of the manual.