# Package for lazy concatenation along arbitrary higher dimensions?

**URL:** https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413
**Category:** General Usage
**Tags:** lazy-evaluation, splitapplycombine
**Created:** [February 16, 2021, 7:15pm UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413 "2021-02-16T19:15:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)
#### Post date: [February 16, 2021, 7:15pm UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413/1 "2021-02-16T19:15:25Z")

</div>

Are there any packages that let you concatenate lazily along arbitrary dimensions like the eager `cat` does?

```julia
julia> mats = [rand(5,6,7,8), rand(5,6,7,8)];

julia> combo = cat(mats..., dims=3); # this, but lazy

julia> size(combo)
(5, 6, 14, 8)

```

In my case, each array in `mats` is very large (sometimes not even in memory), but it would be very convenient to be able to concatenate them into one large array lazily.

`LazyArrays.jl` appears to be the closest, but AFAICT it only supports lazy concatenation along the first or second dimension.

---

<div class="post-metadata">

### Author: ![sunbubble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sunbubble/32/22022_2.png) [@sunbubble](https://discourse.julialang.org/u/sunbubble)
#### Post date: [February 16, 2021, 7:48pm UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413/2 "2021-02-16T19:48:12Z")

</div>

You might be able to express this as a transducer. Checkout [GitHub - JuliaFolds/Transducers.jl: Efficient transducers for Julia](https://github.com/JuliaFolds/Transducers.jl).  
Transducers are expression compositions which you only evaluate once you call `collect`. Maybe a crude analogy, think of `xf = 1:10` as the transducer, and then you call `collect(xf)` to produce the array. However, transducers allow you to express many transformations, such as `map`, `filter`, `fold` and many more, in this way. And you can compose them with a piping syntax such as `1:10 |> Map(isodd) |> Map(isprime) |> collect`, meaning the transducer is `xf = 1:10 |> Map(isodd) |> Map(isprime)`.

And I guess you can just keep appending values, and collect them once you need them.

I hope this helps.

---

<div class="post-metadata">

### Author: ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)
#### Post date: [February 16, 2021, 8:30pm UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413/3 "2021-02-16T20:30:24Z")

</div>

> You might be able to express this as a transducer.

Transducers look very interesting and could probably express my problem, but I was hoping to find something that I can use where I could wrap the lazily concatenated arrays in an AxisArray object to use in my analysis as follows

```julia
julia> AxisArray(lazycat(mat..., dims=3), axisinfo...)

```

so that then my current approach would work, only that now `getindex` would figure out lazily where to get each value on access. Anything that returns a `<: AbstractArray` should work.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [February 16, 2021, 9:02pm UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413/4 "2021-02-16T21:02:33Z")

</div>

There are quite a few packages which do this. Mine is called LazyStack.jl, but JuliennedArrays, and RecursiveArrayTools, do similar things.

---

<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, 7:17am UTC](https://discourse.julialang.org/t/package-for-lazy-concatenation-along-arbitrary-higher-dimensions/55413/5 "2021-02-17T07:17:57Z")

</div>

SplitApplyCombine.jl seems an obvious choice here with its `combinedimsview` function that does exactly what you need.
