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
While this works, it relies on the list concatenation operator
++
, which is really slow, and relies on copying the entire list every time it is called.A better approach would be to implement the flattening using the cons
[head | tail]
operator: