Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align Base.Generator, map. Why Base.IteratorSize(zip()) == Base.IsInfinite()? #40756

Open
goretkin opened this issue May 9, 2021 · 2 comments
Milestone

Comments

@goretkin
Copy link
Contributor

goretkin commented May 9, 2021

zip and map (which is defined in terms of zip, by way of Base.Generator) produce results with length equal to the minimum length of the arguments:

julia> map(string, 1:5, 1:3, 1:4)
3-element Vector{String}:
 "111"
 "222"
 "333"

julia> collect(zip(1:5, 1:3, 1:4))
3-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (2, 2, 2)
 (3, 3, 3)

For zip, the minimum length across no iterators is defined as "infinity"
https://github.com/JuliaLang/julia/blame/ff001a4e1f5b7f949f5a2328520a7e78bc1957b4/base/iterators.jl#L388

julia> Base.IteratorSize(zip())
Base.IsInfinite()

which is the definition consistent with a recursive definition of the minimum (min(x, infinity) = x).

But I'm not sure why a 1-argument map is defined:

map(f) = f()

julia> map(string)
""

Related: #39628

@jonas-schulze
Copy link
Contributor

See also discussion starting at #35916 (comment)

@martinholters
Copy link
Member

That map method was added in #17318 together with f.() begin equal to f(). For broadcast, that seems reasonable, as regarding combination of input sizes, 1 is the neutral element there. But thinking about it, having map(f) return an infinite stream of f() would be more consistent with the zip behavior. IIUC, that would basically make map(f, args...) = collect(f(xs...) for xs in zip(args...)) even for empty args, except that the collect then fails. So maybe map(f) actually should throw, but it's lazy cousin Iterators.map(f) should indeed give Iterators.repeated(f())? Or rather Base.Generator(xs -> f(xs...), zip()) so that f is lazily evaluated once per generated element instead of once eagerly upon construction? Indeed, should we add

Generator(f) = Generator(a->f(), zip()) 

as the zero-iterator version of

Generator(f, I1, I2, Is...) = Generator(a->f(a...), zip(I1, I2, Is...))

?

@LilithHafner LilithHafner added this to the 2.0 milestone Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants