Created
October 17, 2016 19:49
-
-
Save jonlunsford/742414b1039993fe9666c98bb550b521 to your computer and use it in GitHub Desktop.
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 | |
@moduledoc """ | |
This module contains custom Citrusbyte implementations of native functions, | |
For fun and profit. | |
All unit tests are in the form of doctests. | |
""" | |
@doc """ | |
Flattens multi dimensional arrays. | |
An array consisting of other nested arrays | |
will be recursively combined to form a single | |
array. The end 'tail' of the array will be added | |
to the end of the resulting array, First In First Out. | |
## Examples | |
iex> Citrusbyte.flatten([]) | |
[] | |
iex> Citrusbyte.flatten([1]) | |
[1] | |
iex> Citrusbyte.flatten([[1,2,[3]],4]) | |
[1,2,3,4] | |
iex> Citrusbyte.flatten([[1,2,[]],4]) | |
[1,2,4] | |
iex> Citrusbyte.flatten([[[1],[2, [3]],[4]],[5]]) | |
[1,2,3,4,5] | |
""" | |
def flatten([]), do: [] | |
def flatten([ head | tail ]) do | |
flatten(head) ++ flatten(tail) | |
end | |
def flatten(head), do: [ head ] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find the full elixir project here: https://github.com/jonlunsford/citrusbyte-flatten