Skip to content

Instantly share code, notes, and snippets.

@laranicolas
Last active November 22, 2017 14:42
Show Gist options
  • Save laranicolas/2cc1b38f0992e116153ab13b493ed6c6 to your computer and use it in GitHub Desktop.
Save laranicolas/2cc1b38f0992e116153ab13b493ed6c6 to your computer and use it in GitHub Desktop.
Ruby flatten implementation
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