Last active
November 22, 2017 14:42
-
-
Save laranicolas/2cc1b38f0992e116153ab13b493ed6c6 to your computer and use it in GitHub Desktop.
Ruby flatten implementation
This file contains hidden or 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
def flatten(items) | |
return items if items.empty? | |
tail = items.pop | |
if tail.kind_of? Array | |
flatten(items) + flatten(tail) | |
else | |
flatten(items) + [tail] | |
end | |
end | |
items = [[1, 2, [3]], 4, []] | |
p flatten(items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment