-
-
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
Implement a filtermap (or flatmap) function to make do-syntax as powerful as list comprehensions #44294
Comments
Internally, a list comprehension I think it would be nicer - that is, more generic - to have functions that work like
Or equivalently (and nicer):
|
Actually I'm thinking now what I really miss is If
Something would be still be required for flattening these vector of Some/Nothing, though:
But it does work with vectors containing 1 or 0 elements , what is kind of hacky
|
@jakobnissen that looks kind of neat, a form of Currying I guess. My point, though, is being able to have a single functor, and solve the problem with a single "do" body, equivalent to a single comprehension call (variable name only defined once). The flatmap approach is also a little bit more powerful than chaining filter and map, actually even more powerful than the comprehension itself. My frustration is that I often start coding with for-loops that later become maps, but then I may need something a little more complex and I see myself requiring a larger refactoring than simply changing the do block body, and maybe the functor from Scala has this amazing for-loop construction where you can very comfortably extend pipelines into more complex things. The do-syntax reminds me of it a bit, but it's not the same. One thing that happens in Scala is precisely that the whole thing is actually "de-sugared" into flatmaps during compilation. I'm wondering now if having a do-able flatmap in Julia would enable an even more powerful and pleasant functional-coding style! |
The
then you remember that
Done! No changes to the return value of the do-block. With I define
and have found it very useful in data processing/analysis workflows. |
JeffBezanson suggests more efficient implementation based on comprehension syntax.
|
How is that more efficient? julia> using FlexiMaps: filtermap as filtermap1 # the map-filter!-map implementation
julia> filtermap2(f, A) = collect(y for x in A for y in (f(x),) if !isnothing(y)) # generator implementation
julia> @btime filtermap1(identity, 1:10000);
15.446 μs (8 allocations: 156.45 KiB)
julia> @btime filtermap2(identity, 1:10000);
101.462 μs (10 allocations: 326.61 KiB) So, my map-filter!-map version is much faster. |
Suggestion: Julia offers
map
andfilter
, which is great, and using the list comprehension we can also perform afiltermap
, egA
filtermap
function would allow us to perform the same operation, using do-syntax for writing the function. The problem is how to define an interface, since you basically would need to define two functions, one for the filter and another for the map. The suggestion is that this could be accomplished by using Some and Nothing, and then we flatten the output to produce the final filtered result.I couldn't find the suggestion anywhere, so here it goes. Hopefully it's not too futile. I only think it's a good idea because right now this is a unique feature of list comprehension, and I feel it would be nice if do-syntax was more on par with it straight out of the box.
The text was updated successfully, but these errors were encountered: