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

tuple group_by into map #21

Open
ndrean opened this issue Aug 16, 2022 · 0 comments
Open

tuple group_by into map #21

ndrean opened this issue Aug 16, 2022 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@ndrean
Copy link
Contributor

ndrean commented Aug 16, 2022

Suppose you have data in the form [{"M1", "C1"}, {"M1", "C2"}, {"M2", "C3"}] and you want a map "grouped by":
[%{c: ["C2", "C1"], m: "M1"}, %{c: ["C3"], m: "M2"}]

A helper function (perhaps not optimum :)

group_assoc = fn list ->
    list
    |> Enum.map(fn {m, c} -> %{m: m, c: c} end)
    # list_maps = [%{m: "M1", c: "C1"}, %{m: "M1", c: "C2"}, %{m: "M2", c: "C3"}]
    |> Enum.group_by(& &1.m)
    |> Enum.map(fn {k, v} ->
      %{m: k, c: Enum.reduce(v, [], fn e, acc -> [e.c | acc] end)}
    end)
  end
@nelsonic nelsonic added enhancement New feature or request technical A technical issue that requires understanding of the code, infrastructure or dependencies labels Aug 16, 2022
@nelsonic nelsonic added the help wanted Extra attention is needed label Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

2 participants