Created
June 17, 2016 13:57
-
-
Save Batou99/68017df5070460b1202e35026eec290a 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 MyList do | |
def flatten([]), do: [] | |
def flatten([head | tail]), do: flatten(head) ++ flatten(tail) | |
def flatten(x), do: [x] | |
end | |
defmodule MyListTest do | |
use ExUnit.Case | |
doctest MyList | |
test "preserves structure" do | |
assert [1, 2, 3] == MyList.flatten([1, 2, 3]) | |
end | |
test "flattens a shallow nested list" do | |
assert [1,2,3,4] == MyList.flatten([1,2,[3],4]) | |
end | |
test "flattens a deeply nested list" do | |
assert [1,2,3,4] == MyList.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