Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created August 4, 2018 18:10
Show Gist options
  • Save KamilLelonek/c780f436c4143bc3bc8fdf30999b5d7a to your computer and use it in GitHub Desktop.
Save KamilLelonek/c780f436c4143bc3bc8fdf30999b5d7a to your computer and use it in GitHub Desktop.
Flatteing
defmodule List do
def flatten([]), do: []
def flatten([head | tail]), do: flatten(head) ++ flatten(tail)
def flatten(head), do: [head]
end
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