# Is there a way to "enumerate" \`mapslices\` other than\`\`\`i = 0result = mapslices(m

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414>\
**Category:** General Usage\
**Tags:** arrays, splitapplycombine, mapslices\
**Created:** [February 16, 2021, 7:22pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414 "2021-02-16T19:22:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 16, 2021, 7:22pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414/1 "2021-02-16T19:22:17Z")

</div>

Is there a way to “enumerate” `mapslices` other than

```julia
i = 0
result = mapslices(myarray; dims=dims) do x
    i += 1
    myfunction(x, i)
end

```

?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1613501993464400?thread_ts=1613501993.464400&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![bencottier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bencottier/32/20817_2.png) [@bencottier](https://discourse.julialang.org/u/bencottier)\
**Post date:** [February 16, 2021, 7:23pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414/2 "2021-02-16T19:23:37Z")

</div>

(I’m the OP) I’m aware of `eachslice` but unfortunately I’m deliberately avoiding it, because `dims` in `eachslice` has the opposite meaning to that in `mapslices` (e.g. dims=1 \<=\> dims=2), and I want to follow the `mapslices` convention for other reasons.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [February 16, 2021, 7:47pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414/3 "2021-02-16T19:47:46Z")

</div>

Take a look at [JuliennedArrays.jl](https://github.com/bramtayl/JuliennedArrays.jl):

```julia
julia> using JuliennedArrays

julia> v = rand(3,3,3);

julia> dims = (1, 2);

julia> result = map(enumerate(Slices(v, dims...))) do (i, v)
           sum(v)^i
       end
3-element Vector{Float64}:
  3.1860188187847314
 15.902106695465024
 67.67688399859075

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 17, 2021, 9:39am UTC](https://discourse.julialang.org/t/is-there-a-way-to-enumerate-mapslices-other-than-i-0result-mapslices-m/55414/4 "2021-02-17T09:39:06Z")

</div>

This is several times faster - a factor of five for me:

```julia
map(enumerate(splitdimsview(v, 3))) do (i, v)
    sum(v)^i
end

```

That is:

```julia
using SplitApplyCombine, JuliennedArrays, BenchmarkTools

v = rand(100,100,100)

f_ja(v) = map(enumerate(Slices(v, 1, 2))) do (i, v)
	sum(v)^i
end

f_sac(v) = map(enumerate(splitdimsview(v, 3))) do (i, v)
	sum(v)^i
end

julia> @benchmark f_ja($v)
BenchmarkTools.Trial: 
  memory estimate: 1.69 KiB
  allocs estimate: 22
  --------------
  minimum time: 1.614 ms (0.00% GC)
  median time: 1.684 ms (0.00% GC)
  mean time: 1.720 ms (0.00% GC)
  maximum time: 6.179 ms (0.00% GC)
  --------------
  samples: 2906
  evals/sample: 1

julia> @benchmark f_sac($v)
BenchmarkTools.Trial: 
  memory estimate: 896 bytes
  allocs estimate: 1
  --------------
  minimum time: 324.384 μs (0.00% GC)
  median time: 367.401 μs (0.00% GC)
  mean time: 371.568 μs (0.00% GC)
  maximum time: 928.039 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 1

```

But yes, different dims convention compared to mapslices.
