-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Subsumes flatmap functionality under flatten #45985
base: master
Are you sure you want to change the base?
Conversation
@tkf @aplavin @thchr I'd love to hear your thoughts. Although I'm not a fan of sticking to I would like to further illustrate a possible use of flatmap that was perhaps not clear in the previous PR. In other languages there are Optional and Future objects that can be used the following way to perform a kind of function chaining like the following. Suppose you have these two functions that take a normal value and compute it in a Task:
Now we want to compose these functions, we can't simply compose them, because we need a regular value and we get a 'Task'. We need to do something like that:
The idea is that you could "compose" these with flatmap like this:
I know this may sound weird, unusual or futile to programmers who have never met the idea before. I'm just trying to raise awareness of this important programming pattern that is out there, and that I believe could be useful for Julia programmers. Now this function isn't really performing what can be easily understood as "flattening" a list of lists. It's doing something different. That's why it's nice to have a different name. |
Maybe this should be a PR and an issue? My suggestion at #44792 (comment) was only to avoid making a whole new verb for Making yet another verb for |
@mcabbott there's no new verb, no
I see this as an array-specific form of Edit: I wasn't familiar with how I don't understand how you don't understand it's related to cat, it's clearly related, but not the same thing. It's also certainly close to |
Correct, this is a new symbol, and is what I suggest ought to be discussed separately from the rest of the PR. This one is quick, while an eager julia> xs = (rand(100) for _ in 1:100);
julia> @btime stack($xs);
min 22.000 μs, mean 37.096 μs (102 allocations, 165.67 KiB)
julia> @btime collect(Iterators.flatten($xs));
min 88.833 μs, mean 130.521 μs (110 allocations, 414.11 KiB) Obviously |
I see the question of what functions should be available, and what should be their names as very distinct to how they should be implemented to improve its performance. |
You can make the case for |
I'm only proposing this PR to have "flatmap" functionality in I'm very happy with #44792 was reviewed, supported, questioned, discussed, approved and merged. I can't stop anyone from making another PR that reverses it. I would just worry about the future of a project where one might witness something akin to a wikipedia edit war. |
A proposal following comments to #44792. This patch removes the new
Iterators.flatmap
and implements the same functionality as a newflatten
method that can take a function parameter.flatten
is also introduced toBase
similar tomap
, producing concrete arrays.I personally don't really like the name
flatten
. I believe many programmers are probably not very familiar with the great importance of flatmap when you're coding in this style that relies more on functional combinators than on procedural for-loops. That means, for those following this style, we'll be writing a lot of code using the termflatten
where there's something else happening than just flattening. We are flat-mapping. It would be great to have a more accurate term.This patch also extends what was done in #44792 by bringing
flatten
toBase
, producing concrete vectors throughcollect
.