Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: concatenation function for iterables #45563

Closed
mikmoore opened this issue Jun 2, 2022 · 2 comments
Closed

Feature request: concatenation function for iterables #45563

mikmoore opened this issue Jun 2, 2022 · 2 comments

Comments

@mikmoore
Copy link
Contributor

mikmoore commented Jun 2, 2022

Currently we have vcat, hcat, and the slightly more versatile cat function for performing concatenation of varargs. But we do not have /proper/ functions for efficiently concatenating collections of items.

For example, to concatenate a vector of vectors into a matrix one should use reduce(hcat,x::Vector{Vector{T}}) where T. However, this isn't really the reduce function. This method has been special-cased for this specific purpose. It feels more like a pun, since it never actually calls hcat at all. One can also splat hcat(vectorofvectors...), but this has wretched performance.

Personally, I hate specializations like reduce(::typeof(hcat),...) and would love for these to eventually be removed. They thwart the programmers ability to reason about what may or may not be effective (without the specialization, the reduce approach would be horrible) with an encyclopedic knowledge of what specializations are available.

The reduce specializations work for this purpose, but are highly undiscoverable. Most people unaware of the specializations (and most people are unaware, in my experience) tend to reach for a splatted hcat. Further, these specializations are limited to just those two functions in that exact context. How do I concatenate a Vector{Matrix{T}} into an Array{T,3}? There is no builtin (that I am aware of) to do this efficiently and readably. I can start doing grotesque things like reshape(reduce(hcat,vectorofmatrices),size(first(vectorofmatrices))...,:) but I thought I left those monstrosities with MATLAB.

We have minimum for reducing a collection using min and many other such functions, but nothing for concatenation. I propose a function concat(itr;dims) (or concatenate) to generalize cat (which handles arbitrary dimensions - also multiple dimensions simultaneously but that is more than an initial implementation would need) to iterables. In keeping with similar functions, perhaps a predicate could be considered, but that implementation might be tricky given that the predicate might change the size of the items in a way that makes preallocation overly difficult. One can just use concat([f(x) for x in itr];dims) in the meantime.

Looking for discussion on name, functionality, and what else might be missing here. I've got a backlog of other PRs to work on in the near future, so someone feel free to go ahead and take a shot if you're feeling inspired.

@mbaz
Copy link
Contributor

mbaz commented Jun 3, 2022

See #21672.

@mikmoore
Copy link
Contributor Author

Closing due to basically being a dup of #21672 and would be addressed by #43334.

@mikmoore mikmoore closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants