Skip to content

Instantly share code, notes, and snippets.

@Kottakji
Last active October 21, 2018 18:55
Show Gist options
  • Save Kottakji/b7f015166dee1a8a079e9bb9c4d26e56 to your computer and use it in GitHub Desktop.
Save Kottakji/b7f015166dee1a8a079e9bb9c4d26e56 to your computer and use it in GitHub Desktop.
Flatten in elixir without library functions
defmodule MyList do
def flatten([]) do
[]
end
def flatten(value, []) when is_integer(value) do
value
end
def flatten([head | []], enumerates) when is_list(enumerates) do
[head | flatten(enumerates)]
end
def flatten([head | tail]) when is_integer(head) and is_list(tail) do
[head | flatten(tail)]
end
def flatten([head | tail], enumerate) when is_integer(head) and is_integer(tail) and is_list(enumerate) do
[ head, tail | flatten(enumerate)]
end
def flatten([head | tail], enumerate) when is_integer(head) and is_list(tail) and is_list(enumerate) do
[ head | flatten(tail, enumerate)]
end
def flatten([head | []]) when is_list(head) do
flatten(head)
end
def flatten([head | tail]) when is_list(head) and is_list(tail) do
flatten(head, tail)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment