@@ -309,6 +309,26 @@ Examples:
309
309
say ('hello', 1, 22/7, 42, 'world').grep: Int; # OUTPUT: «(1 42)»
310
310
say grep { .Str.chars > 3 }, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(hello 3.142857 world)»
311
311
312
+
313
+ Note that if you want to grep for elements that do not match, you can
314
+ use a C < none > -L < Junction|/type/Junction > :
315
+
316
+ say <a b 6 d 8 0>.grep(none Int); # OUTPUT: «(a b d)»
317
+ say <a b c d e f>.grep(none /<[aeiou]>/); # OUTPUT: «(b c d f)»
318
+
319
+ Another option to grep for elements that do not match a regex is to
320
+ use a block:
321
+
322
+ say <a b c d e f>.grep({! /<[aeiou]>/}) # OUTPUT: «(b c d f)»
323
+
324
+ The reason the example above works is because a regex in boolean
325
+ context applies itself to C < $_ > . In this case, C < ! > boolifies the
326
+ C « /<[aeiou]>/ » regex and negates the result. Smartmatching against a
327
+ L < Callable > (in this case a L < Block > ) returns the value returned from
328
+ that callable, so the boolified result of a regex is then used to
329
+ decide whether the current value should be kept in the result of a
330
+ grep.
331
+
312
332
The optional named parameters C < :k > , C < :kv > , C < :p > , C < :v > provide the same
313
333
functionality as on slices:
314
334
@@ -338,12 +358,6 @@ Examples:
338
358
say grep { .Str.chars > 3 }, :p, 'hello', 1, 22/7, 42, 'world';
339
359
# OUTPUT: «(0 => hello 2 => 3.142857 4 => world)»
340
360
341
- Note that if you want to grep for elements that do not match a regular
342
- expression, you should use the following syntax, with a block:
343
-
344
- say <a b c d e f>.grep({! /<[aeiou]>/})
345
- # OUTPUT: «b c d f»
346
-
347
361
= head2 routine first
348
362
349
363
Defined as:
0 commit comments