Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active August 29, 2023 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/b6f65eabca959271bb97f40a6157ca55 to your computer and use it in GitHub Desktop.
Save myobie/b6f65eabca959271bb97f40a6157ca55 to your computer and use it in GitHub Desktop.
Allow collecting into an integer for sums in elixir
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