tors 23 juni 2016 kl 00:01 skrev Peter Hamilton <[email protected]>:
> Enum.group_map_reduce(list, group_key_fun, map_fun, initial_acc, reducer)
>
> Enum.group_map_reduce([1,1,2,3,5,5,8], &(&1), &(&1), 0, fn _, acc -> acc +
> 1 end)
>
> That would produce:
>
> %{1 => 2, 2 => 1, 3 => 1, 5 => 2, 8 => 1}
>
The whole point here is to do some transformation on all unique values,
right? So we can group unique values and transform them?
I feel like this is a straightforward `list |> Enum.group_by(& &1) |>
Enum.map(&transformation/1)`. Possibly piped to Map.new.
For the case quoted above: `[1,1,2,3,5,5,8] |> Enum.group_by(& &1) |>
Enum.map(fn {k, v} -> {k, Enum.count v} end) |> Map.new`.
I feel like this is well-composed of what we already have in Enum. There
are a bunch these "pre-composed" functions in Enum, like `reverse_slice`,
`map_join`, `filter_map` etc. It's unclear to me when and how the need for
these pre-composed functions arise. Are they all performance optimisations
to have common compositions done in one pass? Is this particular case
common enough to be included as `group_map_reduce`?
Anyway, I would advice against the name `merge` or `merge_by`, as it
carries the connotation of combining two enumerables, the way `Map.merge`
does.
- Martin
--
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/CAAHw6C%2BM47dureeMaQNvKEM3jcag8PBwJWrqi7f%3D705i7%2BTe1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.