Skip to content

Instantly share code, notes, and snippets.

@Batou99
Created June 17, 2016 13:57
Show Gist options
  • Save Batou99/68017df5070460b1202e35026eec290a to your computer and use it in GitHub Desktop.
Save Batou99/68017df5070460b1202e35026eec290a to your computer and use it in GitHub Desktop.
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