Created
June 5, 2015 13:18
-
-
Save rbishop/70f0fbc5377f1d3e60bb to your computer and use it in GitHub Desktop.
TCO and append-less List.flatten
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 ListOp do | |
def flatten([head | tail]), do: flatten(tail, [head]) | |
def flatten([], acc), do: Enum.reverse(acc) | |
def flatten([ [head | []] | tail], acc), do: flatten(tail, [head | acc]) | |
def flatten([ [head | rest] | tail], acc), do: flatten(flatten([rest | tail], [head | acc])) | |
def flatten([head | tail], acc), do: flatten(tail, [head | acc]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment