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

map with different length iterators #13361

Closed
boathit opened this issue Sep 29, 2015 · 10 comments
Closed

map with different length iterators #13361

boathit opened this issue Sep 29, 2015 · 10 comments
Labels
good first issue Indicates a good issue for first-time contributors to Julia Hacktoberfest Good for Hacktoberfest participants

Comments

@boathit
Copy link

boathit commented Sep 29, 2015

Is it reasonable that map can deal with infinite length iterators well but fails to work with two different finite length iterators?

map(+, [1,2,3], repeated(5))

3-element Array{Any,1}:
 6
 7
 8


map(+, [1,2,3], repeated(5, 10))

3-element Array{Any,1}:
 6
 7
 8


map(+, 1:3, repeated(5, 10))

3-element Array{Any,1}:
 6
 7
 8


works well

 map(+, [1,2,3], 1:10)

ERROR: DimensionMismatch("dimensions must match")
 in map at abstractarray.jl:1310

map(+, 1:3, 1:10)

ERROR: DimensionMismatch("dimensions must match")
 in map at abstractarray.jl:1310


error occurs

@ihnorton
Copy link
Member

Suggested alternative:

map(+, [1,2,3], 1:10...)

I'm unsure if it would be a good idea to auto-expand the range 1:10 object (a.k.a. colon(1,10)) in cases like this.

(there are a some similar cases where such auto-expansion might be convenient -- but possibly undesirable globally -- such as indexing a KeyIterator: keys(Dict(1=>"a"))[1] -> MethodError. instead requires: [keys(Dict(1=>"a"))...][1])

@boathit
Copy link
Author

boathit commented Sep 30, 2015

map(+, [1,2,3], 1:10...) will be expanded into map(+, [1,2,3], 1,2,3,4,5,6,7,8,9,10) which departs from the original intention that expects to perform map(+, [1,2,3], [1,2,3,4,5,6,7,8,9,10])

@kshyatt kshyatt added the good first issue Indicates a good issue for first-time contributors to Julia label Jan 8, 2016
@kshyatt kshyatt added the Hacktoberfest Good for Hacktoberfest participants label Oct 5, 2016
@JeffBezanson
Copy link
Member

Maybe this should be allowed for 1-d arrays of different lengths?

@raghav9-97
Copy link
Contributor

@JeffBezanson Is it logically correct if map(f, x, y) work with different lengths of x and y and if length(x) < length(y) the resulting array size should be length(x)?

@raghav9-97
Copy link
Contributor

I also think that issue #29523 is related to this problem.

@timholy
Copy link
Member

timholy commented Dec 7, 2018

An orthogonal solution is to define new iterators like repeatlast(x) which turns a container x into an infinite-length iterator, "padding" with the final element; one could also define Iterators.truncate(y, x) so that a longer iterator y matches a shorter iterator x. I think this would be much more explicit about intent, and also more flexible.

@raghav9-97
Copy link
Contributor

raghav9-97 commented Dec 7, 2018

The promote_shape(::Indices, ::Indices) function works just opposite to what has been suggested for Iterators.truncate(y, x).So instead of promote_shape map should make a call to Iterators.truncate(y, x) function?

@timholy
Copy link
Member

timholy commented Dec 7, 2018

No, promote_shape has a very different purpose:

julia> promote_shape((3, 5), (2, 6))
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape(::Tuple{Int64,Int64}, ::Tuple{Int64,Int64}) at ./indices.jl:103
 [2] top-level scope at none:0

@jonas-schulze
Copy link
Contributor

IIUC this should be fixed by #29927:

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

@mschauer
Copy link
Contributor

Yes, the reported issue was fixed by that:

julia> map(+, [1,2,3], 1:10)
3-element Array{Int64,1}:
 2
 4
 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia Hacktoberfest Good for Hacktoberfest participants
Projects
None yet
Development

No branches or pull requests

9 participants