Last active
March 12, 2018 23:01
-
-
Save mbenatti/7d456fa52258d208a9ae52202f93ce69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ListFlatten do | |
def flatten(list), do: flatten(list, []) |> Enum.reverse | |
def flatten([h | t], acc) when h == [], do: flatten(t, acc) | |
def flatten([h | t], acc) when is_list(h), do: flatten(t, flatten(h, acc)) | |
def flatten([h | t], acc), do: flatten(t, [h | acc]) | |
def flatten([], acc), do: acc | |
end | |
list_1 = [[1,2,[3]],4] | |
list_2 = [[1,5,[3]],4,[[[1,2,[3]],4]]] | |
IO.puts("Running #{inspect list_1}...") | |
IO.inspect(ListFlatten.flatten(list_1)) | |
IO.puts("Running #{inspect list_2}...") | |
IO.inspect(ListFlatten.flatten(list_2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment