tldr
tldr

Reputation: 12112

Elixir: how to make struct enumerable

I have a struct:

my_struct = %MyStruct{a: 1, b: 2}

how do I make it enumerable, so I can use the Enum methods on it?

Upvotes: 6

Views: 4481

Answers (2)

smathy
smathy

Reputation: 27961

Just came across this question and just wanted to add that (a few months after it was answered) we now have Map.from_struct/1:

iex(10)> %MyStruct{a: 1, b: 2} |> Map.from_struct |> Enum.count
2

Upvotes: 1

bitwalker
bitwalker

Reputation: 9251

You can use Map.to_list(struct) instead of Enum.to_list, since structs are just maps with a __struct__ key.

Upvotes: 7

Related Questions