Created
August 4, 2018 18:10
-
-
Save KamilLelonek/c780f436c4143bc3bc8fdf30999b5d7a to your computer and use it in GitHub Desktop.
Flatteing
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 List do | |
def flatten([]), do: [] | |
def flatten([head | tail]), do: flatten(head) ++ flatten(tail) | |
def flatten(head), do: [head] | |
end |
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 ListTest do | |
use ExUnit.Case, async: true | |
describe "flatten" do | |
test "should flatten an empty list" do | |
assert [] = List.flatten([]) | |
end | |
test "should flatten an one-element list" do | |
list = [1] | |
assert ^list = List.flatten(list) | |
end | |
test "should wrap a single element" do | |
element = 1 | |
assert [^element] = List.flatten(element) | |
end | |
test "should flatten a list" do | |
list = [[1, 2, [3]], 4] | |
assert [1, 2, 3, 4] == List.flatten(list) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment