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 Enumerable — Elixir v1.12.0-dev 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.

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

al2o3cr

al2o3cr

I don’t understand this part - if you’re passing a struct where the third-party code expects an Enumerable, why couldn’t you just… not do that?

vrod

vrod

Hmm… all of your comments make me inspect refactoring more seriously. Maybe my solution is improper. Thank you for your guidance! I am still learning!

Where Next?

Popular in Questions Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

We're in Beta

About us Mission Statement