Last active
August 29, 2023 16:31
-
-
Save myobie/b6f65eabca959271bb97f40a6157ca55 to your computer and use it in GitHub Desktop.
Allow collecting into an integer for sums in elixir
This file contains 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
defmodule ExtInteger do | |
defimpl Collectable, for: Integer do | |
@doc """ | |
Collect into: an integer | |
## Examples | |
iex> for n <- 1..5, into: 0, do: n | |
15 | |
""" | |
def into(num) do | |
fun = fn | |
acc, {:cont, n} -> acc + n | |
acc, :done -> acc | |
_, :halt -> :ok | |
end | |
{num, fun} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment