Continuing on this nice dicussion about reduce would there be a way to use the same syntax while applying another function, if at al relevant.
Basically, I have some calls that look like reduce(vcat, fun.(array_of_array)) where I want to apply fun to all items in array_of_array. Given that I am not interested in the result of a single call of fun, would it make a difference to have something like a reduce(vcat, a -> fun(a), array_of_array) ?
I am not familiar enough with reduce to say if something already exists or not to do thatβ¦
Note that reduce(vcat, fun.(array)) may well hit a special method for vectors of vectors or matrices, which allocates one array to write into. There is no such method for mapreduce(fun, vcat, array), and thus it will call vcat many times, allocating length(array)-1 arrays in the process. This will probably be much more expensive than the one temporary array from fun.(array).
If fun(a) returns a vector, then you can try vec(stack(fun, array)), with using Compat first.
Thatβs becaues reduce can check the sizes beforehand to know how much memory to allocate, but mapreduce cannot know the sizes of arrays output by the user function before it is called.
This is a strong statement and I only half mean it, but⦠I kind of wish I had never learned about mapreduce. I reach for it quite often now but I usually find out that it is doing things in a pretty inefficient way (because of the shortcomings listed here).
This is true, although it could still do better than pairwise vcat, for instance by guessing that all are the same size as the first. Or by repeated doubling like append!.
Indeed, this is one of the things which makes stack easier.
Mostly nobody has got around to it. PR 31636 had a go. Another option would simply be to send this to reduce(vcat, map(f, xs)), as this will usually be better. (This is also what happens for mapreduce(f, op, xs, ys) at present.)
The other option would be to write a flatten companion for stack, since these clever reduce overloads are hard to discover & quite fragile β itβs easy to step off the fast path, as with init keyword for example.