Created
June 17, 2016 13:53
-
-
Save Batou99/4d9bdb8ca5cec0eb763b3295bc54ccb3 to your computer and use it in GitHub Desktop.
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 Flatten do | |
def flatten([]), do: [] | |
def flatten([head | tail]), do: flatten(head) ++ flatten(tail) | |
def flatten(x), do: [x] | |
end | |
defmodule FlattenTest do | |
use ExUnit.Case | |
doctest Flatten | |
test "preserves structure" do | |
assert [1, 2, 3] == Flatten.flatten([1, 2, 3]) | |
end | |
test "flattens a shallow nested list" do | |
assert [1,2,3,4] == Flatten.flatten([1,2,[3],4]) | |
end | |
test "flattens a deeply nested list" do | |
assert [1,2,3,4] == Flatten.flatten([[1,2,[3]],4]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment