Avatar of davearonson

davearonson's solution

to Flatten Array in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

Running tests

Execute the tests with:

$ elixir flatten_array_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.

Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

flatten_array_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("flatten_array.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule FlattenArrayTest do
  use ExUnit.Case

  test "returns original list if there is nothing to flatten" do
    assert FlattenArray.flatten([1, 2, 3]) == [1, 2, 3]
  end

  @tag :pending
  test "flattens an empty nested list" do
    assert FlattenArray.flatten([[]]) == []
  end

  @tag :pending
  test "flattens a nested list" do
    assert FlattenArray.flatten([1, [2, [3], 4], 5, [6, [7, 8]]]) == [1, 2, 3, 4, 5, 6, 7, 8]
  end

  @tag :pending
  test "removes nil from list" do
    assert FlattenArray.flatten([1, nil, 2]) == [1, 2]
  end

  @tag :pending
  test "removes nil from a nested list" do
    assert FlattenArray.flatten([1, [2, nil, 4], 5]) == [1, 2, 4, 5]
  end

  @tag :pending
  test "returns an empty list if all values in nested list are nil" do
    assert FlattenArray.flatten([nil, [nil], [nil, [nil]]]) == []
  end
end
defmodule Flattener do
  @doc """
    Accept a list and return the list flattened without nil values.

    ## Examples

      iex> Flattener.flatten([1, [2], 3, nil])
      [1,2,3]

      iex> Flattener.flatten([nil, nil])
      []

  """

  @spec flatten(list) :: list
  def flatten(list) do
    do_flatten(list, [])
  end

  defp do_flatten([], sofar), do: sofar

  defp do_flatten([head|tail], sofar) when is_list(head) do
    do_flatten(head, sofar) ++ do_flatten(tail, sofar)
  end

  defp do_flatten([nil |tail], sofar), do:         do_flatten(tail, sofar)
  defp do_flatten([head|tail], sofar), do: [head | do_flatten(tail, sofar)]

end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?