Skip to content

Instantly share code, notes, and snippets.

@jcelliott
Last active August 26, 2021 16:21
Show Gist options
  • Save jcelliott/cdd94a3ebdfc850d3e92ba17ccb447a3 to your computer and use it in GitHub Desktop.
Save jcelliott/cdd94a3ebdfc850d3e92ba17ccb447a3 to your computer and use it in GitHub Desktop.
Recursively flatten a list in Elixir

You can run the tests with elixir flatten_test.exs:

$ elixir flatten_test.exs 

FlattenTest
  * test nested array 2 (0.00ms)
  * test empty array (0.00ms)
  * test nested array 3 (0.00ms)
  * test nested array 1 (0.00ms)
  * test one element (0.00ms)
  * test non-nested array (0.00ms)
  * test nested array 4 (0.00ms)


Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
7 tests, 0 failures

Randomized with seed 969009
defmodule Flatten do
@doc """
Flattens a list by recursively flattening the head and tail of the list
"""
def flatten([head | tail]), do: flatten(head) ++ flatten(tail)
def flatten([]), do: []
def flatten(element), do: [element]
end
Code.load_file("flatten.exs", __DIR__)
ExUnit.start
ExUnit.configure trace: true
defmodule FlattenTest do
use ExUnit.Case
test "empty array" do
assert Flatten.flatten([]) == []
end
test "one element" do
assert Flatten.flatten([1]) == [1]
end
test "non-nested array" do
assert Flatten.flatten([1, 2, 3]) == [1, 2, 3]
end
test "nested array 1" do
assert Flatten.flatten([[1, 2, [3]], 4]) == [1, 2, 3, 4]
end
test "nested array 2" do
assert Flatten.flatten([[1], [2, [3]], [4, 5]]) == [1, 2, 3, 4, 5]
end
test "nested array 3" do
assert Flatten.flatten([1, [2, [3, [4, [5]]]]]) == [1, 2, 3, 4, 5]
end
test "nested array 4" do
assert Flatten.flatten([[[[[1], 2], 3], 4], 5]) == [1, 2, 3, 4, 5]
end
end
@heri16
Copy link

heri16 commented Aug 26, 2021

The above may overflow the stack.
See tail recursion version that I wrote: https://gist.github.com/heri16/e726ee7f335d2ca61bbbb016e6b884e1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment