Created
May 22, 2013 18:12
-
-
Save sasa1977/5629646 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 MyLc do | |
def next({_, :stop}, _), do: [] | |
def next([], _), do: [] | |
def next([current | next], fun) do | |
[fun.(current) | next(next, fun)] | |
end | |
def next({iter, {current, next}}, fun) do | |
[fun.(current) | next({iter, iter.(next)}, fun)] | |
end | |
defmacro mylc({var, _,[{:inenum, _, [enumerable]}]}, do: block) do | |
quote do | |
MyLc.next( | |
Enum.Iterator.iterator(unquote(enumerable)), | |
fn(unquote({var, [], nil})) -> unquote(block) end | |
) | |
end | |
end | |
end | |
defmodule Test do | |
import MyLc | |
def test do | |
IO.inspect(mylc x inenum 1..5 do | |
x + 1 | |
end) | |
IO.inspect(mylc x inenum [2,3,4] do | |
x * x | |
end) | |
end | |
end | |
Test.test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment