Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created September 14, 2016 02:41
Show Gist options
  • Save santosh79/9f1aed60bc8880de091d59b7b5fa844d to your computer and use it in GitHub Desktop.
Save santosh79/9f1aed60bc8880de091d59b7b5fa844d to your computer and use it in GitHub Desktop.
defmodule ArrayFlattener do
def flatten(list) do
flatten(list, []) |> :lists.reverse
end
def flatten([], acc) do; acc; end
def flatten([head|rest], acc) when is_list(head) do
flatten rest, flatten(head, acc)
end
def flatten([head|rest], acc) do
flatten rest, [head|acc]
end
end
defmodule ArrayFlattenerTest do
use ExUnit.Case
test "the truth" do
assert ArrayFlattener.flatten([[1, 2], 3]) === [1, 2, 3]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment