Julia 0.7 flattening of Nested Tuples

I’ve implemented a Julia Version of a “Multiplicative Partitions of a Number”. Since 0.7 the State, start … iteration changed. I got a pre 0.7 Version of a Tuple flattener on this Page: Nested list comprehensions in Julia - Stack Overflow

function flatten(x, y)
      state = start(x)
       if state==false
          push!(y, x)
    else
        while !done(x, state) 
          (item, state) = next(x, state) 
          flatten(item, y)
        end 
    end
    y
end
flatten(x)=flatten(x,Array(Any, 0))

This doesn’t work anymore in 0.7, and the alternative:

collect(Iterators.flatten(Iterators.flatten([(1,2,[3,3,3,3]),[4,5],6])))

only works to a nest-level of 4, while i need to flatten the tuples to an arbitrary depth.
Can someone tell me how to alter the flatten function code to work with 0.7?

1 Like

Found it:

function flatten(arr)
	rst = Any[]
	grep(v) = 	for x in v
				if isa(x, Tuple) 
				grep(x) 
				else push!(rst, x) end
				end
	grep(arr)
	rst
end
1 Like