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

Open
boathit opened this issue Sep 29, 2015 · 8 comments

Comments

6 participants
@boathit
Copy link

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

This comment has been minimized.

Copy link
Member

commented Sep 29, 2015

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

This comment has been minimized.

Copy link
Author

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])

@JeffBezanson

This comment has been minimized.

Copy link
Member

commented Feb 24, 2018

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

@raghav9-97

This comment has been minimized.

Copy link
Contributor

commented Dec 7, 2018

@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

This comment has been minimized.

Copy link
Contributor

commented Dec 7, 2018

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

@timholy

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.