Created
November 30, 2017 22:47
-
-
Save jxub/ef5ad0aad555ca896366dea51d3581b5 to your computer and use it in GitHub Desktop.
A way to add implementation of Enum for Tuples in Elixir 😁
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defimpl Enumerable, for: Tuple do | |
def count(tuple) do | |
tuple_size(tuple) | |
end | |
def member?([], _), do {:ok, false} | |
def member?({}, _), do {:ok, false} | |
def member?(tuple, elem) do | |
tuple | |
|> Tuple.to_list | |
|> member?(elem) | |
end | |
def slice({}, _start, _count), do: {} | |
def slice(tuple, start, count), do | |
tuple | |
|> Tuple.to_list | |
|> slice(start, count) | |
end | |
def reduce(_, {:halt, acc}, _fun), do: {:error, __MODULE__} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment