Last active
September 1, 2017 21:54
-
-
Save mareksuscak/acb4de6f72b2e6983c7c22b64bf7ce8a to your computer and use it in GitHub Desktop.
Idiomatic deep flatten implementation in Elixir.
This file contains hidden or 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 Array do | |
def flatten(list), do: flatten(list, []) | |
def flatten([head | tail], acc) when head == [], do: flatten(tail, acc) | |
def flatten([head | tail], acc) when is_list(head), do: flatten(tail, flatten(head, acc)) | |
def flatten([head | tail], acc), do: flatten(tail, acc ++ [head]) | |
def flatten([], acc), do: acc | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copied from here.
@mareksuscak
++
is very expensive operation. Check out this benchmark: