Lulu Arrays – Flattening
Introduction
If you have imported the lulu.Array
class as
local Array = require 'lulu.Array'
Then, you can use flatten an Array
using the method:
Array:flatten(depth))
Returns a new Array
which is a flattened version of self
.
The default value for the depth
parameter is 1.
Example:
local a = Array{{1,2}, {3,4}, {5,6}}
("a: %t", a)
putln("a:flatten(): %t", a:flatten()) putln
Outputs:
a: [ [1, 2], [3, 4], [5, 6] ] a:flatten(): [ 1, 2, 3, 4, 5, 6 ]
Another Example:
local a = Array{{{1,2}}, {{{3,4}}}, {5,6}}
("a:flatten(): %t", a:flatten()) putln
Outputs [ [ 1, 2 ], [ [ 3, 4 ] ], 5, 6 ]
.
For the same array, a:flatten(2)
will give [ 1, 2, [3, 4], 5, 6 ]
, and a:flatten(3)
will give [ 1, 2, 3, 4, 5, 6 ]
which is also the output from a:flatten(n)
for \(n > 3\).