# 'map' function error with different length iterators

**URL:** <https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421>\
**Category:** Internals & Design\
**Created:** [December 7, 2018, 3:05pm UTC](https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421 "2018-12-07T15:05:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [December 7, 2018, 3:05pm UTC](https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421/1 "2018-12-07T15:05:26Z")

</div>

Map function when applied on different length iterators throws a DimensionMismatch error while it works perfectly fine when applied to infinite length iterators.

The strategy of map in case of infinite length iterator can be generalised as well to different length iterators i.e. to apply map element wise and return array whose size is minimum of the two.

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [December 7, 2018, 3:06pm UTC](https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421/2 "2018-12-07T15:06:57Z")

</div>

A strategy suggested by @tim.holy was to implement Iterators.truncate(y, x) function which will truncate longer y iterator to the size of x.

---

<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:** [December 7, 2018, 3:21pm UTC](https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421/3 "2018-12-07T15:21:09Z")

</div>

Note `IterTools.shortest` which I think implements the trait selection you are looking for.

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [December 7, 2018, 4:15pm UTC](https://discourse.julialang.org/t/map-function-error-with-different-length-iterators/18421/5 "2018-12-07T16:15:21Z")

</div>

The map function makes a call to promote\_shape which works differently while we need to compare the two iterators length and return one with the less length.
