I don’t like the name, but the idea here would be for
`Enum.operation_by/4`, wouldn’t it, or would it be a function that returns
a function?
Something like:
```elixir
def operation_by(operation, transform_fun, filter_fun)
fn enumerable ->
filtered = Enum.filter(enumerable, filter_fun)
apply(Enum, operation, [filtered, transform_fun])
end
end
def operation_by(enumerable, operation, transform_fun, filter_fun)
filtered = Enum.filter(enumerable)
apply(Enum, operation, [filtered, transform_fun])
end
```
So this would visibly replace `enum |> filter(filter_fun) |>
map(transform_fun)` as `enum |> operation_by(:map, transform_fun,
filter_fun)`.
I’m not sure that this offers _enough_ visual benefit (and operation_by
isn’t exactly a catchy name) that it would be worth adding to core. If it
could _also_ provide a _performance_ benefit, I think it’d be an easier
win. Especially as a filter-and-transform can be done pretty easily with
`Enum.reduce`:
```elixir
list
|> Enum.reduce([], fn value, acc ->
if Integer.mod(value, 3) == 0 do
[ value * value | acc ]
else
acc
end)
|> Enum.reverse()
```
-a
On Tue, Jan 12, 2021 at 7:32 AM Luca Campobasso <[email protected]>
wrote:
> There are already some functions that take a filter function with the
> operation, like `Enum.count/1` and its filtered version, or `Enum.count/2`.
> Some other functions, like `Enum.max` or `Enum.frequencies` have a `_by`
> version that takes an additional argument.
>
> It would be cool and much more succinct, also for the library, if we could
> generalise this behavior to all functions, and have a function that takes a
> function name as an atom, its "operating" function, and a filter function,
> i.e. something like this:
> ```
> Enum.operation_by(:map, &String.downcase/1, &filter_fun/1)
> ```
> which I think would consistently reduce the dimension of the API. People
> often use filtering before doing stuff with Enum. This doesn't bring any
> new function, but rather generalises a behaviour and reduces the amount of
> stuff one would need to remember or know about the Enum API (which by the
> way, it's a work of art as it is TBH).
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/d6ae0579-04d7-4a2c-944a-f7279c789004n%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/d6ae0579-04d7-4a2c-944a-f7279c789004n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
Austin Ziegler • [email protected] • [email protected]
http://www.halostatue.ca/ • http://twitter.com/halostatue
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQvbrSryLwqrpr%2Ba%2Btt9fTk-R%3DmmpUjUneA%3DVdq1GDpOoQ%40mail.gmail.com.