Le mercredi 12 octobre 2016 01:45:25 UTC+2, Jared Crean a écrit :
> Very nice summary, thanks for posting.  One question I had was what should 
> the signature of a function be to receive a generator?  For example, if the 
> only method of extrema is extrema(A::AbstractArray), is that too 
> restrictive?
>   Jared Crean
Any functions working with iterables will work with generators. 

julia> methods(extrema)
# 4 methods for generic function "extrema":
extrema(r::Range) at reduce.jl:345
extrema(x::Real) at reduce.jl:346
extrema(A::AbstractArray, dims) at reduce.jl:388
extrema(itr) at reduce.jl:362

The last line tells you that extrema will work. An object is iterable if it 
implements the methods start, next and done. There are in fact a few other 
objects that also work on generators.

 julia> methodswith(Base.Generator)
8-element Array{Method,1}:
 collect(itr::Base.Generator) at array.jl:298 
 done(g::Base.Generator, s) at generator.jl:22
 indices(g::Base.Generator) at generator.jl:91
 length(g::Base.Generator) at generator.jl:89 
 ndims(g::Base.Generator) at generator.jl:92  
 next(g::Base.Generator, s) at generator.jl:24
 size(g::Base.Generator) at generator.jl:90   
 start(g::Base.Generator) at generator.jl:21  

There are a few functions that work on arrays but not on iterables. You 
should not expect these to work on generators.

julia> show(reverse([1:10;]))
julia> show(reverse(i for i = 1:10))
ERROR: MethodError: no method matching 
Closest candidates are:
  reverse(!Matched::String) at strings/string.jl:209
  reverse(!Matched::BitArray{1}) at bitarray.jl:1416
  reverse(!Matched::Tuple) at tuple.jl:199

Reply via email to