# Is there a \`slices\` function?

**URL:** <https://discourse.julialang.org/t/is-there-a-slices-function/80724>\
**Category:** General Usage\
**Tags:** views\
**Created:** [May 8, 2022, 9:41pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724 "2022-05-08T21:41:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [May 8, 2022, 9:41pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724/1 "2022-05-08T21:41:38Z")

</div>

Is there a lazy equivalent to `mapslices` that returns a sliced view into an array?

We have `sortperm` and `sortslices`, but I’d like to get the permutation that puts slices in order. I’d like to accomplish this with `sortperm(slices(x, dims=2))`.

If `x::AbstractArray` with `ndims(x)=3`, then I’d expect `slices(x, dims=2)` to return `y::AbstractMatrix{<:AbstractVector}` which shares data with `x`.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 8, 2022, 9:45pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724/2 "2022-05-08T21:45:40Z")

</div>

I think you are looking for `eachslice(x, dims = 2)`

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [May 8, 2022, 9:51pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724/3 "2022-05-08T21:51:28Z")

</div>

Thanks!

I was hoping for something one level more like a view (i.e. `y[2] = y[4]` would have side effects on x) but this is likely the answer I’m looking for. For the specific use case I presented, `sortperm(collect(eachslice(x, dims=2)))` should work (I’ll just need to provide my own `lt` opperator to compare matrices)

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [May 9, 2022, 1:52pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724/4 "2022-05-09T13:52:54Z")

</div>

You’ll be interested in [add Slices array type for eachslice/eachrow/eachcol by simonbyrne · Pull Request #32310 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/32310), which adds such a view type.

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [May 9, 2022, 1:59pm UTC](https://discourse.julialang.org/t/is-there-a-slices-function/80724/5 "2022-05-09T13:59:08Z")

</div>

Yes! Thanks! That is exactly what I’m looking for! I hope it lands soon.
