Examples collection
/ P07 - Flatten a nested array structure.
P07 - Flatten a nested array structure.
We use the gather/take structure.
A gather block builds a list using take statements. Each take adds one
element to the list. The gather block returns the complete list.
A gather block operates with dynamic scope, so the take statements may be
in another subroutine.
iterates over @t, placing each element in turn in $t
is the smart match operator. Here we use it to check the type of $t.
we could have said $t.isa(Array) instead.
Specification
P07 (**) Flatten a nested array structure.
Transform an array, possibly holding arrays as elements into a `flat'
list by replacing each array with its elements (recursively).
Example
> splat([1,[2,[3,4],5]]).perl.say;
(1, 2, 3, 4, 5)
use v6;
sub _splat(@t) {
for @t -> $t {
if $t ~~ Array { _splat($t) }
else { take $t }
}
}
sub splat (@t) { gather _splat(@t) }
splat([1, [2,[3,4], 5]]).list.perl.say;
# vim: expandtab shiftwidth=4 ft=perl6
See Also P08-topo.raku
P08 - Eliminate consecutive duplicates of list elements.
P09-rje.raku
P09 - Pack consecutive duplicates of list elements into sublists.
P09-scottp.raku
P09 - Pack consecutive duplicates of list elements into sublists.
P09-topo.raku
P09 - Pack consecutive duplicate elements of a list into sublists.
P09-unobe.raku
P09 - Pack consecutive duplicates of list elements into sublists.
P15-rhebus.raku
P15 - Replicate the elements of a list a given number of times.
P15-topo.raku
P15 - Replicate the elements of a list a given number of times.
P15-unobe.raku
P15 - Replicate the elements of a list a given number of times.
P17-sdondley.raku
P17 - Split a list into two parts; the length of the first part is given.
P17-topo.raku
P17 - Split a list into two parts; the length of the first part is given.
P17-unobe.raku
P17 - Split a list into two parts; the length of the first part is given.
P18-topo.raku
P18 - Extract a slice from a list. Indices start at 1.
P21-topo.raku
P21 - Insert an element at a given position into a list.
P22-scottp.raku
P22 - Create a list containing all integers within a given range.
P22-topo.raku
P22 - Create a list containing all integers within a given range.
P23-topo.raku
P23 - Extract a given number of randomly selected elements from a list.
P24-topo.raku
P24 - Draw N different random numbers from the set 1..M.
P25-topo.raku
P25 - Generate a random permutation of the elements of a list.
P26-topo.raku
P26 - Generate the combinations of k distinct objects chosen from the n elements of a list.
P32-rhebus.raku
P32 - Determine the greatest common divisor of two positive integer
P33-rhebus.raku
P33 - Determine whether two positive integer numbers are coprime.
P35-rhebus.raku
P35 - Determine the prime factors of a given positive integer.
P36-ovid.raku
P36 - Determine the prime factors of a given positive integer (2).
P36-rhebus.raku
P36 - Determine the prime factors of a given positive integer (2).
š¦ / Examples collection
/ P07 - Flatten a nested array structure.
The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society.
All rights reserved.
Built with Podlite ā the markup and publishing tools behind this site.