Skip to content

Instantly share code, notes, and snippets.

@PragTob
Last active June 15, 2016 18:53
Show Gist options
  • Save PragTob/546d586fb000cc80c08d564c65235dfd to your computer and use it in GitHub Desktop.
Save PragTob/546d586fb000cc80c08d564c65235dfd to your computer and use it in GitHub Desktop.
defmodule MyMap do
def map_tco(list, function) do
Enum.reverse _map_tco([], list, function)
end
defp _map_tco(acc, [head | tail], function) do
_map_tco([function.(head) | acc], tail, function)
end
defp _map_tco(acc, [], _function) do
acc
end
def map_tco_concat(acc \\ [], list, function)
def map_tco_concat(acc, [head | tail], function) do
map_tco_concat(acc ++ [function.(head)], tail, function)
end
def map_tco_concat(acc, [], _function) do
acc
end
def map_body([], _func), do: []
def map_body([head | tail], func) do
[func.(head) | map_body(tail, func)]
end
def map_tco_no_reverse(list, function) do
_map_tco([], list, function)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment