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
module ArrayTransform | |
# Recursively flatten an array by flattening each half of the input array and concatenating the results. | |
# Special cases: | |
# 1) A zero-length array is returned unchanged | |
# 2) An array nested inside a single-element array is flattened and returned. | |
# Terminating condition: An array containing a single non-array-element is returned without modification | |
def self.flatten(obj) | |
raise "flatten: Array required; #{obj.class} provided" unless obj.is_a?(Array) | |
olen = obj.length # because we use it multiple times |