Sha256: 26f629e3817fb501883f13b24399c4f4b1b7e783a38eeacdd893f948ae6d08b9

Contents?: true

Size: 640 Bytes

Versions: 1

Compression:

Stored size: 640 Bytes

Contents

import java.util.ArrayList
import java.util.List
import java.util.Collection

def flatten(list: Collection) 
    flatten(list, ArrayList.new)
end
def flatten(source: Collection, result: List)

    source.each do |x|
        if (Collection.class.isAssignableFrom(x.getClass())) 
            flatten(Collection(x), result)  
        else
            result.add(x)
            result  # if branches must return same type
        end 
    end
    result
end

# creating a list-of-list-of-list fails currently, so constructor calls are needed
source = [[1], 2, [[3, 4], 5], [[ArrayList.new]], [[[6]]], 7, 8, ArrayList.new]

puts flatten(source)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mirah-0.0.8-java examples/rosettacode/flatten-a-list.mirah