vrod

vrod

How to implement Enumerable protocol for struct?

Sorry because many other people asked a similar question… but how do you implement the Enumerable protocol for a struct? I see the docs on https://hexdocs.pm/elixir/master/Enumerable.html but I do not follow how to actually do this… is there an example somewhere? Just to be able to treat my structs the same as maps for many Enum operations. Thank you!

Most Liked

seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

Why not just use Map.from_struct/1?

a = %MyStruct{foo: 0, bar: 1}
a
|> Map.from_struct()
|> Enum.count()
brettbeatty

brettbeatty

  1. the code that calls this is in 3rd party package - I cannot pre-change my struct

I would just echo what the others have said–there might be a way to grab the needed data out of your structs before passing it to the other library. Typically you don’t want to implement protocols for data types for which the protocol doesn’t really make sense (what does it mean to enumerate a datetime?).

  1. academic. I want to know how to do this

I think the code that really made things click for me was the Enumable implementation for Range.

I also tried writing a blog post about implementing Enumerable for custom data structures, but I don’t know if it’s any clearer.

eksperimental

eksperimental

The best example is the source code itself.
https://github.com/elixir-lang/elixir/blob/ae5188d8996f9ab44358be3d7acd4447a2de0677/lib/elixir/lib/enum.ex#L3986-L4007

But note that you cannot rely on the implementation for maps. The only function that will work is member?/2

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement