Skip to content

Instantly share code, notes, and snippets.

@Batou99
Created June 17, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Batou99/4d9bdb8ca5cec0eb763b3295bc54ccb3 to your computer and use it in GitHub Desktop.
Save Batou99/4d9bdb8ca5cec0eb763b3295bc54ccb3 to your computer and use it in GitHub Desktop.
defmodule Flatten do
def flatten([]), do: []
def flatten([head | tail]), do: flatten(head) ++ flatten(tail)
def flatten(x), do: [x]
end
defmodule FlattenTest do
use ExUnit.Case
doctest Flatten
test "preserves structure" do
assert [1, 2, 3] == Flatten.flatten([1, 2, 3])
end
test "flattens a shallow nested list" do
assert [1,2,3,4] == Flatten.flatten([1,2,[3],4])
end
test "flattens a deeply nested list" do
assert [1,2,3,4] == Flatten.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