Skip to content

Commit 32faa8e

Browse files
committed
Suggest none-junction, explain why !/…/ works
Hopefully resolves #1425.
1 parent 30f6e1a commit 32faa8e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

doc/Type/List.pod6

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,26 @@ Examples:
309309
say ('hello', 1, 22/7, 42, 'world').grep: Int; # OUTPUT: «(1 42)␤»
310310
say grep { .Str.chars > 3 }, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(hello 3.142857 world)␤»
311311
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+
312332
The optional named parameters C<:k>, C<:kv>, C<:p>, C<:v> provide the same
313333
functionality as on slices:
314334
@@ -338,12 +358,6 @@ Examples:
338358
say grep { .Str.chars > 3 }, :p, 'hello', 1, 22/7, 42, 'world';
339359
# OUTPUT: «(0 => hello 2 => 3.142857 4 => world)␤»
340360
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-
347361
=head2 routine first
348362
349363
Defined as:

0 commit comments

Comments
 (0)