asnyder002
asnyder002

Reputation: 125

How can I create nested group_by?

Essentially I'm trying to take data and use Enum.group_by to create a dictionary but I'd like to continue to group the same data by sub categories.

data = [
  %{company: "company_one", state: "LA", size: 100},
  %{company: "company_one", state: "LA", size: 200},
  %{company: "company_two", state: "TX", size: 200},
  %{company: "company_two", state: "LA", size: 300},
  %{company: "company_three", state: "LA", size: 400},
  %{company: "company_four", state: "TX", size: 500}
]

I want to first group by company then within that map group those now by state and once again, within that nested map group them by size. Essentially a map with two nested maps and an array of the corresponding data.

%{"company_one" => %{
   "LA" => %{
      "100" => [
         %{company: company_one, state: LA, size: 100}
         ],
      "200" => [
         %{company: company_one, state: LA, size: 200}
         ]
      }
   }
}

My attempt looks something like this

list = 
   data
   |> Enum.group_by(fn x -> x.company end)

keys = Map.keys(data)

updated_list = 
    for key <- keys do
       list[key]
       |> Enum.group_by(fn x -> x.state end)
    end

I've used Enum.group_by/1 for the initial format but anything i've tried after that really messes up the data structure. Any help would be great. Thanks.

Upvotes: 1

Views: 499

Answers (2)

Leonardo Dagnino
Leonardo Dagnino

Reputation: 3215

I found this question interesting, and ended making a generic way to do this - just pass your structure and the keys you want to group by.

defmodule NestedGroup do
  defp access_keys([key]), do: [Access.key(key, [])]
  defp access_keys([key | rest]), do: [Access.key(key, %{}) | access_keys(rest)]

  def nested_group(data, keys) do
    data
    |> Enum.reduce(%{}, fn elt, acc ->
      keys =
        Enum.map(keys, &Map.get(elt, &1))
        |> access_keys()

      update_in(acc, keys, fn list -> [elt | list] end)
    end)
  end
end

We need the access_key helper to make sure the last key is a list, instead of a map.

iex(2)> NestedGroup.nested_group(data, [:company, :state, :size])
%{
  "company_four" => %{
    "TX" => %{500 => [%{company: "company_four", size: 500, state: "TX"}]}
  },
  "company_one" => %{
    "LA" => %{
      100 => [%{company: "company_one", size: 100, state: "LA"}],
      200 => [%{company: "company_one", size: 200, state: "LA"}]
    }
  },
  "company_three" => %{
    "LA" => %{400 => [%{company: "company_three", size: 400, state: "LA"}]}
  },
  "company_two" => %{
    "LA" => %{300 => [%{company: "company_two", size: 300, state: "LA"}]},
    "TX" => %{200 => [%{company: "company_two", size: 200, state: "TX"}]}
  }
}

Upvotes: 2

Adam Millerchip
Adam Millerchip

Reputation: 23091

It's not very pretty, but you could do it like this:

Enum.group_by(data, & &1.company)
|> Map.new(fn {k, v} ->
  {k,
   Enum.group_by(v, & &1.state)
   |> Map.new(fn {k, v} -> {k, Enum.group_by(v, & &1.size)} end)}
end)

Result:

%{
  "company_four" => %{
    "TX" => %{500 => [%{company: "company_four", size: 500, state: "TX"}]}
  },
  "company_one" => %{
    "LA" => %{
      100 => [%{company: "company_one", size: 100, state: "LA"}],
      200 => [%{company: "company_one", size: 200, state: "LA"}]
    }
  },
  "company_three" => %{
    "LA" => %{400 => [%{company: "company_three", size: 400, state: "LA"}]}
  },
  "company_two" => %{
    "LA" => %{300 => [%{company: "company_two", size: 300, state: "LA"}]},
    "TX" => %{200 => [%{company: "company_two", size: 200, state: "TX"}]}
  }
}

Upvotes: 3

Related Questions