Created
September 14, 2016 02:41
-
-
Save santosh79/9f1aed60bc8880de091d59b7b5fa844d to your computer and use it in GitHub Desktop.
This file contains 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 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