@@ -10,63 +10,106 @@ X<|\() (Capture)>
10
10
= for code
11
11
class Capture { }
12
12
13
- A C < Capture > is a container for passing arguments to a code object. Captures
14
- are the flip-side of L < Signature|/type/Signature > s – Captures are the caller defined arguments,
15
- while Signatures are the callee defined parameters.
16
-
17
- When you call C < print $a, $b > , the C < $a, $b > part is a Capture.
13
+ A C < Capture > is a container for passing arguments to a code object. Captures
14
+ are the flip-side of L < Signature|/type/Signature > s. Thus, captures are the
15
+ caller-defined arguments, while signatures are the callee-defined parameters.
16
+ For example when you call C < print $a, $b > , the C < $a, $b > part is a capture.
18
17
19
18
Captures contain a list-like part for positional arguments and a hash-like part
20
- for named arguments, thus behaving as L < Positional|/type/Positional > and L < Associative|/type/Associative > , although
21
- it does not actually mixes in those roles. For the named arguments, Captures
22
- use a slightly different I < syntax > than a normal List. There are two easy ways
23
- to make a named argument: 1) use an unquoted key naming a parameter, followed by
24
- C « => » , followed by the argument and 2) use a colon-pair literal named after the
25
- parameter:
26
-
27
- say unique 1, -2, 2, 3, as => { abs $_ }; # OUTPUT: «(1 -2 3)»
28
- # ... is the same thing as:
29
- say unique 1, -2, 2, 3, :as({ abs $_ }); # OUTPUT: «(1 -2 3)»
30
- # Be careful not to quote the name of a named parameter:
31
- say unique 1, -2, 2, 3, 'as' => { abs $_ };
32
- # OUTPUT: «(1 -2 2 3 as => -> ;; $_? is raw { #`(Block|78857320) ... })»
33
-
34
- A stand-alone Capture can also be made, stored, and used later. A literal
35
- Capture can be created by prefixing a term with a backslash C < \ > .
36
- Commonly, this term will be a List of terms, from which any Pair
37
- literal will be placed in the named part, and all other terms will be
38
- placed in the positional part.
39
-
40
- my $c = \(42); # Capture with one positional part
41
- $c = \(1, 2, a => 'b'); # Capture with two positional and one named parts
42
-
43
- To use such a Capture, you may use C < '|' > before it in a function call, and it
44
- will be as if the values in the Capture were passed directly to the function
45
- as arguments – named arguments will be passed as named arguments and positional
46
- arguments will be passed as positional arguments. You may re-use the Capture
47
- as many times as you want, even with different functions.
48
-
49
- my $c = \(4, 2, 3);
50
- reverse(|$c).say; # OUTPUT: «3 2 4»
51
- sort(5,|$c).say; # OUTPUT: «2 3 4 5»
52
-
53
- Inside a Signature, a Capture may be created by prefixing a sigilless parameter
54
- with a vertical bar C < | > . This packs the remainder of the argument list
55
- into that parameter.
56
-
57
- f(1, 2, 3, a => 4, b => 5);
19
+ for named arguments, thus behaving as L < Positional|/type/Positional > and
20
+ L < Associative|/type/Associative > , although it does not actually mixes in those
21
+ roles. Like any other data structure, a stand-alone capture can be created,
22
+ stored, and used later.
23
+
24
+ A literal C < Capture > can be created by prefixing a term with a backslash C < \ > .
25
+ Commonly, this term will be a L « List|/type/List » of terms, from which
26
+ the forms C « key => value » and C « :key<value> » of a L « C < Pair > |/type/Pair» literal
27
+ will be placed in the named part, and all other terms will be placed in the
28
+ positional part (including C < Pair > s of the form C « 'key' => value » ).
29
+
30
+ my $a = \(42); # Capture with one positional arg
31
+ my $b = \(1, 2, verbose => True); # Capture with two positional args and one named arg
32
+ my $c = \(1, 2, :verbose(True)); # same as before
33
+ my $c = \(1, 2, 'verbose' => True); # Capture with three positional args
34
+
35
+ To reiterate, named arguments in a capture must be created using one of two
36
+ ways:
37
+
38
+ = item Use an I < unquoted > key naming a parameter, followed by C « => » , followed by
39
+ the argument. For example, C « as => by => {1/$_} » .
40
+
41
+ = item Use a L < colon-pair|/language/glossary#Colon_pair_and_colon_list > literal
42
+ named after the parameter. For example, C < :into(my %leap-years) > .
43
+
44
+ For example:
45
+
46
+ sub greet(:$name, :$age) {
47
+ "$name, $age"
48
+ }
49
+
50
+ my $d = \(name => 'Mugen', age => 19); # OK
51
+ my $e = \(:name('Jin'), :age(20)); # OK
52
+ my $f = \('name' => 'Fuu', 'age' => 15); # Not OK, keys are quoted.
53
+
54
+ For the C < greet > subroutine that accepts two named arguments C < name > and
55
+ C < age > , the captures C < $d > and C < $e > will work fine while the capture C < $f >
56
+ will throw a C < Too many positionals passed... > error. This is because
57
+ C « 'age' => 20 » isn't a named argument (as per the two ways of creating one
58
+ mentioned above) but a positional argument of which C < greet > expects none. In
59
+ the context of captures, quoted keys don't create named arguments. Any C « 'key'
60
+ => value » is just another positional parameter, thus exercise some caution when
61
+ creating captures with named arguments.
62
+
63
+ Once a capture is created, you may use it by prefixing it with a vertical bar
64
+ C < | > in a subroutine call, and it will be as if the values in the capture were
65
+ passed directly to the subroutine as arguments — named arguments will be passed
66
+ as named arguments and positional arguments will be passed as positional
67
+ arguments. You may re-use the capture as many times as you want, even with
68
+ different subroutines.
69
+
70
+ say greet |$d; # OUTPUT: «Mugen, 19»
71
+ say greet |$e; # OUTPUT: «Jin, 20»
72
+
73
+ my $x = \(4, 2, 3, -2);
74
+ say reverse |$x; # OUTPUT: «(-2 3 2 4)»
75
+ say sort 5, |$x; # OUTPUT: «(-2 2 3 4 5)»
76
+
77
+ say unique |$x, as => {.abs}; # OUTPUT: «(4 2 3)»
78
+ say unique |$x, :as({.abs}); # OUTPUT: «(4 2 3)»
79
+
80
+ my $y = \(1, 7, 3, by => {1/$_});
81
+ say min |$y; # OUTPUT: «7», same as min 1, 7, 3, by => {1/$_}
82
+ say max |$y; # OUTPUT: «1», same as max 1, 7, 3, by => {1/$_}
83
+
84
+ Inside a C < Signature > , a C < Capture > may be created by prefixing a
85
+ L < sigilless parameter|/language/variables#Sigilless_variables > with a
86
+ vertical bar C < | > . This packs the remainder of the argument list into that
87
+ L < capture parameter|/type/Signature#Capture_parameters > .
88
+
58
89
sub f($a, |c) {
59
- # c is \(2, 3, a => 4, b => 5)
90
+ say $a;
91
+ say c;
92
+ say c.^name;
93
+ say c.list; # see Methods section
94
+ say c.hash; # see Methods section
60
95
}
61
96
62
- Note that Captures are still Lists in that they may contain containers, not
63
- just values:
97
+ f 1, 2, 3, a => 4, :b(5);
98
+ # OUTPUT:
99
+ # 1
100
+ # \(2, 3, :a(4), :b(5))
101
+ # Capture
102
+ # (2 3)
103
+ # Map.new((a => 4, b => 5))
104
+
105
+ Note that C < Capture > s are still C < List > s in that they may contain containers,
106
+ not just literal values:
64
107
65
108
my $b = 1;
66
109
my $c = \(4, 2, $b, 3);
67
- sort( |$c).say ; # OUTPUT: «1 2 3 4 »
68
- $b = 6 ;
69
- sort( |$c).say ; # OUTPUT: «2 3 4 6 »
110
+ say min |$c; # OUTPUT: «1»
111
+ $b = -5 ;
112
+ say min |$c; # OUTPUT: «-5 »
70
113
71
114
= head1 Methods
72
115
@@ -76,18 +119,18 @@ Defined as:
76
119
77
120
method list(Capture:D:)
78
121
79
- Returns the positional part of the Capture.
122
+ Returns the positional part of the C < Capture > .
80
123
81
124
my Capture $c = \(2, 3, 5, apples => (red => 2));
82
- say $c.list; # OUTPUT: «(2 3 5)»
125
+ say $c.list; # OUTPUT: «(2 3 5)»
83
126
84
127
= head2 method hash
85
128
86
129
Defined as:
87
130
88
131
method hash(Capture:D:)
89
132
90
- Returns the named/hash part of the Capture.
133
+ Returns the named/hash part of the C < Capture > .
91
134
92
135
my Capture $c = \(2, 3, 5, apples => (red => 2));
93
136
say $c.hash; # OUTPUT: «Map.new((:apples(:red(2))))»
@@ -98,10 +141,10 @@ Defined as:
98
141
99
142
method elems(Capture:D: --> Int:D)
100
143
101
- Returns the number of positional elements in the Capture.
144
+ Returns the number of positional elements in the C < Capture > .
102
145
103
146
my Capture $c = \(2, 3, 5, apples => (red => 2));
104
- say $c.elems; # OUTPUT: «3»
147
+ say $c.elems; # OUTPUT: «3»
105
148
106
149
= head2 method keys
107
150
@@ -114,7 +157,7 @@ named keys. For positional arguments the keys are the respective arguments
114
157
ordinal position starting from zero.
115
158
116
159
my $capture = \(2, 3, 5, apples => (red => 2));
117
- say $capture.keys; # OUTPUT: «(0 1 2 apples)»
160
+ say $capture.keys; # OUTPUT: «(0 1 2 apples)»
118
161
119
162
= head2 method values
120
163
@@ -126,7 +169,7 @@ Returns a L<Seq|/type/Seq> containing all positional values followed by all
126
169
named argument values.
127
170
128
171
my $capture = \(2, 3, 5, apples => (red => 2));
129
- say $capture.values; # OUTPUT: «(2 3 5 red => 2)»
172
+ say $capture.values; # OUTPUT: «(2 3 5 red => 2)»
130
173
131
174
= head2 method kv
132
175
@@ -139,7 +182,7 @@ L<values|#method_values>. The positional keys and values, if any, comes
139
182
first followed by the named keys and values.
140
183
141
184
my $capture = \(2, 3, apples => (red => 2));
142
- say $capture.kv; # OUTPUT: «(0 2 1 3 apples red => 2)»
185
+ say $capture.kv; # OUTPUT: «(0 2 1 3 apples red => 2)»
143
186
144
187
= head2 method pairs
145
188
@@ -153,7 +196,7 @@ their respective ordinal value, starting at zero, as key while the
153
196
named arguments have their names as key.
154
197
155
198
my Capture $c = \(2, 3, apples => (red => 2));
156
- say $c.pairs; # OUTPUT: «(0 => 2 1 => 3 apples => red => 2)»
199
+ say $c.pairs; # OUTPUT: «(0 => 2 1 => 3 apples => red => 2)»
157
200
158
201
= head2 method antipairs
159
202
@@ -168,19 +211,19 @@ the value. This behavior is the opposite of the L<pairs|#method_pairs>
168
211
method.
169
212
170
213
my $capture = \(2, 3, apples => (red => 2));
171
- say $capture.antipairs; # OUTPUT: «(2 => 0 3 => 1 (red => 2) => apples)»
214
+ say $capture.antipairs; # OUTPUT: «(2 => 0 3 => 1 (red => 2) => apples)»
172
215
173
216
= head2 method Bool
174
217
175
218
Defined as:
176
219
177
220
method Bool(Capture:D: --> Bool:D)
178
221
179
- Returns C < True > if the Capture contains at least one named or one
222
+ Returns C < True > if the C < Capture > contains at least one named or one
180
223
positional argument.
181
224
182
- say \(1,2,3, apples => 2).Bool; # OUTPUT: «True»
183
- say \().Bool; # OUTPUT: «False»
225
+ say \(1,2,3, apples => 2).Bool; # OUTPUT: «True»
226
+ say \().Bool; # OUTPUT: «False»
184
227
185
228
= head2 method Capture
186
229
@@ -198,9 +241,9 @@ Defined as:
198
241
199
242
method Numeric(Capture:D: --> Int:D)
200
243
201
- Returns the number of positional elements in the Capture.
244
+ Returns the number of positional elements in the C < Capture > .
202
245
203
- say \(1,2,3, apples => 2).Numeric; # OUTPUT: «3»
246
+ say \(1,2,3, apples => 2).Numeric; # OUTPUT: «3»
204
247
205
248
= end pod
206
249
0 commit comments