Created
September 21, 2016 23:02
-
-
Save et/e0de23f49d685e47995a5639ffeae679 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 CitrusByte do | |
@moduledoc """ | |
Provides the `flatten/1` function to flatten a List | |
""" | |
@doc """ | |
Flattens a List | |
## Examples | |
iex> CitrusByte.flatten([[1,2,[3]],4]) | |
[1,2,3,4] | |
""" | |
@spec flatten(List.t) :: List.t | |
def flatten([]), do: [] | |
def flatten([head|tail]), do: flatten(head) ++ flatten(tail) | |
def flatten(head), do: [head] | |
end |
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 CitrusByteTest do | |
use ExUnit.Case | |
doctest CitrusByte | |
test "flatten" do | |
assert [1,2,3,4] == CitrusByte.flatten([1,2,3,4]) | |
assert [1,2,3,4] == CitrusByte.flatten([[1,2,[3]],4]) | |
assert [1,2,3,4] == CitrusByte.flatten([[1,[2,[3]]],4]) | |
assert [] == CitrusByte.flatten([]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment