Use a tuple to get indexed access to a arguments

This commit is contained in:
Felipe Sere
2016-03-17 23:13:35 +00:00
parent 89dac5bb05
commit 4897527dab
3 changed files with 21 additions and 2 deletions

View File

@@ -1,7 +1,8 @@
defmodule Koans do
defmacro koan(name, body) do
compiled_name = String.to_atom(name)
mangled_body = ASTMangler.expand(body, quote do: answer)
expanded_answers = expand(ASTMangler.count(body))
mangled_body = ASTMangler.expand(body, expanded_answers)
quote do
@koans unquote(compiled_name)
def unquote(compiled_name)() do
@@ -13,13 +14,18 @@ defmodule Koans do
end
end
def unquote(compiled_name)(answer) do
def unquote(compiled_name)(answer) when is_list(answer) do
converted = List.to_tuple(answer)
unquote(mangled_body)
:ok
end
end
end
def expand(amount) do
Enum.map(0..amount, fn (idx) -> {:elem, [context: Koans, import: Kernel], [{:converted, [], Koans}, idx]} end)
end
defmacro __using__(_opts) do
quote do
@compile :nowarn_unused_vars

View File

@@ -8,6 +8,11 @@ defmodule ASTManglerTest do
assert {:+, [context: ASTManglerTest, import: Kernel], [1, 37]} == mangled
end
test "Work with multiple different replacements" do
[koan | _] = SampleKoan.all_koans
assert :ok == apply(SampleKoan, koan, [{:multiple, [3,4]}])
end
def complex_example do
[head | tail] = [1,2,3,4]

View File

@@ -0,0 +1,8 @@
defmodule SampleKoan do
use Koans
koan "thinking more than once" do
assert 3 == :__
assert 4 == :__
end
end