Enum.group_by
on maps works fine:
m = [
%{row: 1, col: 1, txt: "A"},
%{row: 1, col: 2, txt: "B"},
%{row: 2, col: 1, txt: "C"},
%{row: 2, col: 3, txt: "D"},
%{row: 3, col: 1, txt: "X"}
]
mm = Enum.group_by(m, fn(%{row: x}) -> x end, fn(%{txt: t}) -> t end)
for {_, x} <- mm, do: x
gives [["A", "B"], ["C", "D"], ["X"]]
But if each element is a struct e.g., %BotEngine.Action{row: 1, col: 1, txt: "A"},
then it fails with:
** (Protocol.UndefinedError) protocol Enumerable not implemented for %BotEngine.Action{...}
Can we @derive
from Enumerable for a struct?
I have tried this:
defmodule BotEngine.Action do
@derive [Enumerable]
defstruct row: 0,
col: 0,
txt: ""
== Compilation error on file lib/types.ex ==
** (ArgumentError) Enumerable.Any is not available, cannot derive Enumerable for BotEngine.Action