# How to implement Enumerable protocol for struct?

**URL:** <https://forum.elixirforum.com/t/how-to-implement-enumerable-protocol-for-struct/37269>\
**Category:** Questions\
**Tags:** enumerables, structs, protcol\
**Created:** [February 3, 2021, 1:31am UTC](https://forum.elixirforum.com/t/how-to-implement-enumerable-protocol-for-struct/37269 "2021-02-03T01:31:52Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![vrod](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/v/ee7513/32.png) [@vrod](https://forum.elixirforum.com/u/vrod)\
**Post date:** [February 3, 2021, 11:37am UTC](https://forum.elixirforum.com/t/how-to-implement-enumerable-protocol-for-struct/37269/6 "2021-02-03T11:37:59Z")

</div>

Thank you all for the suggestions! @eksperimental this source code was very helpful and @srowley you are right some decisions must be taken about this `: __struct__ ` key.

For example I tried implementing this with `DateTime` struct – I found this is almost 100% copy of the source code for `Map` impl. The only change I tried was in the count function but I understand this may be different depends on what you need to do:

```elixir
defimpl Enumerable, for: DateTime do
  def count(map) do
    {:ok, map |> Map.from_struct() |> map_size()}
  end

  def member?(map, {key, value}) do
    {:ok, match?(%{^key => ^value}, map)}
  end

  def member?(_map, _other) do
    {:ok, false}
  end

  def slice(map) do
    size = map_size(map)
    {:ok, size, &Enumerable.List.slice(:maps.to_list(map), &1, &2, size)}
  end

  def reduce(map, acc, fun) do
    Enumerable.List.reduce(:maps.to_list(map), acc, fun)
  end
end

```

---

_[View the full topic](https://forum.elixirforum.com/t/how-to-implement-enumerable-protocol-for-struct/37269)._
