root/examples/slurpy-list-params/flatten-arg-lists.pl

Revision 11264, 1.0 kB (checked in by Darren_Duncan, 3 years ago)

renamed about 44 more Perl 6 scripts from *.p6 to *.pl ... there are maybe 50-100 more that still need doing

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1"# -------------------------------".say;
2"# flattening arg lists".say;
3"# -------------------------------".say;
4
5sub foo($x, $y, $z) {
6  "x foo $x".say;
7  "y foo $y".say;
8  "z foo $z".say;
9}
10
11sub boo(*$x, *$y, *$z) {
12  "x boo $x".say;
13  "y boo $y".say;
14  "z boo $z".say;
15}
16
17sub goo(*@x, *@y, *@z) {
18  @x[1].say;
19  @y[1].say;
20  @z[1].say;
21}
22
23my @onetothree = 1..3;       # array stores three scalars
24
25foo(1,2,3);                  # okay:  three args found
26
27#foo(@onetothree);           # error: only one arg
28boo(@onetothree);            # ok
29
30##### DOES NOT WORK THOUGH S06 SAYS IT SHOULD:
31#foo(*@onetothree);          # okay:  @onetothree flattened to three args
32#boo(*@onetothree);           # fails
33
34
35# The * operator flattens lazily -- the array is only flattened if
36# flattening is actually required within the subroutine. To flatten
37# before the list is even passed into the subroutine, use the
38# unary prefix ** operator:
39
40#foo(**@onetothree);          # array flattened before &foo called
41
42
Note: See TracBrowser for help on using the browser.