From ed5a059b0278295c4e1a8ef28639c10b6bba2461 Mon Sep 17 00:00:00 2001 From: ekmartin Date: Mon, 7 Sep 2015 13:55:51 +0200 Subject: [PATCH] Add apply, symbol? nil? true? and false? --- elixir/lib/mal/core.ex | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/elixir/lib/mal/core.ex b/elixir/lib/mal/core.ex index 43422204..348a2f39 100644 --- a/elixir/lib/mal/core.ex +++ b/elixir/lib/mal/core.ex @@ -23,10 +23,15 @@ defmodule Mal.Core do "first" => &first/1, "rest" => &rest/1, "map" => &map/1, + "apply" => &apply/1, + "symbol?" => &symbol?/1, "list" => fn args -> args end, + "nil?" => fn [type] -> type == nil end, + "true?" => fn [type] -> type == true end, + "false?" => fn [type] -> type == false end, "read-string" => fn [input] -> Mal.Reader.read_str(input) end, "cons" => fn [prepend, list] -> [prepend | list] end, - "throw" => fn [arg] -> throw({:error, arg}) end + "throw" => fn [arg] -> throw({:error, arg}) end, } end @@ -89,11 +94,23 @@ defmodule Mal.Core do def rest([[head | tail]]), do: tail def rest([[]]), do: [] - def map([{:macro, function}, list]), do: do_map(function, list) - def map([{:closure, function}, list]), do: do_map(function, list) + def map([{_function_type, function}, list]), do: do_map(function, list) def map([function, list]), do: do_map(function, list) defp do_map(function, list) do Enum.map(list, fn arg -> function.([arg]) end) end + + def apply([{_function_type, function} | tail]), do: do_apply(function, tail) + def apply([function | tail]), do: do_apply(function, tail) + + def do_apply(function, tail) do + [list | args] = tail + |> Enum.reverse + func_args = Enum.concat(list, args) + function.(func_args) + end + + def symbol?([{:symbol, _}]), do: true + def symbol?(_), do: false end