Skip to content

Instantly share code, notes, and snippets.

@aaronjensen
Created November 25, 2015 23:16
Show Gist options
  • Select an option

  • Save aaronjensen/cd9ed4cd0c68fd014c18 to your computer and use it in GitHub Desktop.

Select an option

Save aaronjensen/cd9ed4cd0c68fd014c18 to your computer and use it in GitHub Desktop.
defmodule Foo do
def iterate([]) do
:done
end
def iterate([head | tail]) do
{head, fn -> iterate(tail) end}
end
def reduce(coll, acc, fun) do
case iterate(coll) do
{val, next} -> _reduce(next, fun.(val, acc), fun)
:done -> acc
end
end
def _reduce(next, acc, fun) do
case next.() do
{val, next} -> _reduce(next, fun.(val, acc), fun)
:done -> acc
end
end
end
IO.inspect Foo.iterate([3, 4])
IO.inspect Foo.reduce([3, 4], 0, &(&1 + &2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment