Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Created May 22, 2013 18:12
Show Gist options
  • Save sasa1977/5629646 to your computer and use it in GitHub Desktop.
Save sasa1977/5629646 to your computer and use it in GitHub Desktop.
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