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 function inconsistency with scalar inputs #33635

Closed
BioTurboNick opened this issue Oct 22, 2019 · 5 comments
Closed

Map function inconsistency with scalar inputs #33635

BioTurboNick opened this issue Oct 22, 2019 · 5 comments

Comments

@BioTurboNick
Copy link
Contributor

Pursuant to discussion here: https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089

Scalars, provided to a function like map or pmap, leads to unexpected behavior:

map(+, 1:2, 1) # [2]

map(+, 1:2, [1]) # ERROR: DimensionMismatch

A user would naively think a scalar would be broadcast or rejected. Treating it as if the user just wanted to process the first element of each argument would be unlikely to be intended.

Based on discussion linked above, it appears to be because scalars are iterable. The code for consuming iterables uses zip() to stitch the arguments together, which takes as many as it can and discards the remainder.

For comparison, here are how some other inputs are treated:

map(+, 1:2, (1,)) # [2]

map(+, 1:2, (1,2)) # [2;4]

map(+, 1:2, (1,2,3)) # [2;4]

map(+, 1:2, (i for i in 1:1)) # ERROR: DimensionMismatch

map(+, 1:2, (i for i in 1:2)) # [2;4]

These are also surprising to me, since documentation suggests a generator is an iterable, but its size is being enforced as if it were an array. Meanwhile, tuples which have a fixed number of elements are treated like an iterator of unknown length.

@mbauman
Copy link
Member

mbauman commented Oct 22, 2019

Duplicate of #29523

@mbauman mbauman marked this as a duplicate of #29523 Oct 22, 2019
@mbauman mbauman closed this as completed Oct 22, 2019
@BioTurboNick
Copy link
Contributor Author

BioTurboNick commented Oct 22, 2019

It's not a duplicate. That issue is about an array input being checked inconsistently. This issue is about a scalar input not being checked at all.

It's possible that the constellation of fixes proposed for the related issues would also solve this issue, but I'm unclear on that at the moment.

@mbauman
Copy link
Member

mbauman commented Oct 22, 2019

Ok, but can we fold this into that issue, though? Yes, it's a slightly different example, but it's definitely an example of map checking dimensions inconsistently. It'll be easier to track and tackle if it's all together.

@BioTurboNick
Copy link
Contributor Author

Sure. Should I make an explicit note of that in that issue?

@mbauman
Copy link
Member

mbauman commented Oct 22, 2019

That'd be great!

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

2 participants