Last active
February 20, 2017 18:12
-
-
Save deeTEEcee/b627324c277fe49468519e10f1cf84b7 to your computer and use it in GitHub Desktop.
Ruby internals
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
# an imitation of ruby's flatten which takes in a 2nd argument | |
def flatten(array, n=nil) | |
n = -1 if !n | |
while n != 0 | |
is_flat = true | |
array = array.inject([]) do |acc, x| | |
if x.is_a?(Array) | |
is_flat = false | |
acc + x | |
else | |
acc + [x] | |
end | |
end | |
n -= 1 if n | |
return array if is_flat | |
end | |
return array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment