Skip to content

Instantly share code, notes, and snippets.

@rbishop
Created June 5, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbishop/70f0fbc5377f1d3e60bb to your computer and use it in GitHub Desktop.
Save rbishop/70f0fbc5377f1d3e60bb to your computer and use it in GitHub Desktop.
TCO and append-less List.flatten
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