# Map/pmap and scalar argument

**URL:** <https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089>\
**Category:** Internals & Design\
**Created:** [October 19, 2019, 10:25pm UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089 "2019-10-19T22:25:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [October 19, 2019, 10:25pm UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089/1 "2019-10-19T22:25:21Z")

</div>

Take this code:

```julia
function foo(arg1, arg2, arg3)
   arg1 + arg2 + arg3
end

map(foo, [1;2;3], [4;5;6], 5)

```

This code is equivalent to `[foo(1, 4, 5)]`, the other values discarded.

Is this behavior desired for any reason? It doesn’t appear to be documented, and I naively thought that a scalar would be broadcasted.

If `c` was an array, there’s a DimensionMismatch error, so it isn’t as if it always takes the smallest set it can apply.

Thus, should `map(foo, [1;2;3], [4;5;6], 5)` throw an error, forcing the user to turn it into an array of the necessary size; or should it broadcast the scalar internally?

Same applies to pmap.

Checked Julia 1.3.0-rc3 as well as 1.2.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 20, 2019, 5:20am UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089/2 "2019-10-20T05:20:17Z")

</div>

An even simpler MWE is

```julia
map(+, 1:2, 1)

```

I suspect it has to do with scalars being iterable, but I am not sure it is intended.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 21, 2019, 7:02am UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089/3 "2019-10-21T07:02:32Z")

</div>

My guess is that this is unintended. It happens because `map` calls `zip`, and `zip` stops iterating once the shortest iterator (i.e. the scalar) is exhausted. (This behavior of `zip` is documented.)

The reason it does not throw a `DimensionMistmatch` is that `Base.IteratorSize(zip(1:2, 1))` hits a fallback function that simply returns `SizeUnknown()` and this causes the dimension check to be bypassed.

Making this an error would be breaking, but I’d say it’s worth it since any code that it breaks is likely to be buggy. Users should be forced to specify either `map(+, first(1:2), 1)` or `map(+, 1:2, Iterators.repeated(1))` depending on what they meant.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 21, 2019, 7:35am UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089/4 "2019-10-21T07:35:00Z")

</div>

I still think the major issue is scalars being iterable. Eg

```julia
julia> Base.IteratorSize(typeof(1:2))
Base.HasShape{1}()

julia> Base.IteratorSize(Number)
Base.HasShape{0}()

```

and when `Base.zip_iteratorsize` falls back to `Base.and_iteratorsize`, [the fallback case](https://github.com/JuliaLang/julia/blob/864e6d63d234031b9b6a0abb524572d69fe91707/base/iterators.jl#L43) delivers a `SizeUnknown()`.

I am not sure what the best way to fix this is, but I do think this is a bug, and fixing a bug is not considered a breaking change.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [October 22, 2019, 2:30pm UTC](https://discourse.julialang.org/t/map-pmap-and-scalar-argument/30089/5 "2019-10-22T14:30:31Z")

</div>

Since you do consider it a bug, I’ll create an issue for it over on GitHub.
