Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. defmodule ListUtilities do
  2.  
  3. @doc """
  4. Flattens the given `list` of nested lists.
  5. ## Examples
  6. iex> ListUtilities.flatten([[1,2,[3]],4])
  7. [4, 3, 2, 1]
  8. """
  9.  
  10. @spec flatten(list()) :: list()
  11. def flatten(list), do: do_flatten(list, [])
  12.  
  13. defp do_flatten([head | tail], acc ) when is_list(head), do: do_flatten(tail, do_flatten(head, acc))
  14. defp do_flatten([head | tail], acc ), do: do_flatten(tail, [head| acc])
  15. defp do_flatten([], acc), do: acc
  16.  
  17. end
  18.  
  19. ListUtilities.flatten([[1,2,[3]],4]) |> IO.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement