Created
April 21, 2017 16:47
-
-
Save kavichu/0cc917eb795fc30d8836e2d2de15719b to your computer and use it in GitHub Desktop.
Elixir noob code to flatten array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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