Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Forked from veelenga/flatten.exs
Created September 15, 2019 01:15
Show Gist options
  • Save snewcomer/08db85bfdfa758d38837fe276c229968 to your computer and use it in GitHub Desktop.
Save snewcomer/08db85bfdfa758d38837fe276c229968 to your computer and use it in GitHub Desktop.
Flattening array in elixir
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment