Created
November 25, 2015 23:16
-
-
Save aaronjensen/cd9ed4cd0c68fd014c18 to your computer and use it in GitHub Desktop.
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
| 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