Skip to content

Instantly share code, notes, and snippets.

@kavichu
Created April 21, 2017 16:47
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 kavichu/0cc917eb795fc30d8836e2d2de15719b to your computer and use it in GitHub Desktop.
Save kavichu/0cc917eb795fc30d8836e2d2de15719b to your computer and use it in GitHub Desktop.
Elixir noob code to flatten array
defmodule Citrusbyte do
def flatten([head | tail]) do
if(is_list(head)) do
flatten(head) ++ flatten(tail)
else
[head] ++ flatten(tail)
end
end
def flatten([]) do
[]
end
end
nested = [[4, 5, 6, [1, 2, [1, 2, 3], 3]], 1, 2, 3]
IO.inspect Citrusbyte.flatten(nested)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment