Last active
December 11, 2017 12:09
-
-
Save alejandrobabio/4578ed310cb4a16cea7902f8f454a99e to your computer and use it in GitHub Desktop.
Using flat_map to flatten a nested array
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
flat = (l = ->(x) { x.flat_map {|e| Array === e ? l.(e) : e } }) | |
flat.([1, 2, 3, [4, 5, [6], [ ] ] ]) | |
#==> [1,2,3,4,5,6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment