Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested list comprehensions in Julia

Tags:

julia

In python I can do nested list comprehensions, for instance I can flatten the following array thus:

a = [[1,2,3],[4,5,6]]
[i for arr in a for i in arr]

to get [1,2,3,4,5,6]

If I try this syntax in Julia I get:

julia> a
([1,2,3],[4,5,6],[7,8,9])

julia> [i for arr in a for i in arr]
ERROR: syntax: expected ]

Are nested list comprehensions in Julia possible?

like image 638
Mike Vella Avatar asked Jan 15 '14 04:01

Mike Vella


1 Answers

This feature has been added in julia v0.5:

julia> a = ([1,2,3],[4,5,6],[7,8,9])
([1,2,3],[4,5,6],[7,8,9])

julia> [i for arr in a for i in arr]
9-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9
like image 129
Jeff Bezanson Avatar answered Oct 19 '22 22:10

Jeff Bezanson