Created
April 8, 2018 08:25
-
-
Save kvokka/533b1a3462c6a4e6bf45bedb7cfc89cb to your computer and use it in GitHub Desktop.
own flatten
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
def my_flatten(s) | |
s.inject([]) do |arr, el| | |
el.respond_to?(:to_ary) ? my_flatten(arr+el) : arr.push(el) | |
end | |
end | |
samples = [ | |
[[[2,3],[5,6,7],[[8,9],10],11,12]], | |
[1,2,[3],4], | |
[:foo, 'bar', 42, [{baz: 23}]] | |
] | |
samples.all?{ |s| my_flatten(s) == s.flatten } || raise | |
puts 'Done ;-)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment