In List§

See primary documentation in context for routine grep

sub    grep(Mu $matcher, *@elems, :$k, :$kv, :$p, :$v --> Seq:D)
method grep(List:D:  Mu $matcher, :$k, :$kv, :$p, :$v --> Seq:D)

Returns a sequence of elements against which $matcher smartmatches. The elements are returned in the order in which they appear in the original list.

Examples:

say ('hello', 1, 22/7, 42, 'world').grep: Int;              # OUTPUT: «(1 42)␤»
say grep { .Str.chars > 3 }, 'hello', 1, 22/7, 42, 'world'; # OUTPUT: «(hello 3.142857 world)␤»

Note that if you want to grep for elements that do not match, you can use a none-Junction:

say <a b 6 d 8 0>.grep(none Int);           # OUTPUT: «(a b d)␤»
say <a b c d e f>.grep(none /<[aeiou]>/);   # OUTPUT: «(b c d f)␤»

Another option to grep for elements that do not match a regex is to use a block:

say <a b c d e f>.grep({! /<[aeiou]>/})     # OUTPUT: «(b c d f)␤»

The reason the example above works is because a regex in Boolean context applies itself to $_. In this case, ! boolifies the /<[aeiou]>/ regex and negates the result. Smartmatching against a Callable (in this case a Block) returns the value returned from that callable, so the boolified result of a regex is then used to decide whether the current value should be kept in the result of a grep.

The optional named parameters :k, :kv, :p, :v provide the same functionality as on slices:

  • k

Only return the index values of the matching elements in order.

  • kv

Return both the index and matched elements in order.

  • p

Return the index and the matched element as a Pair, in order.

  • v

Only return the matched elements (same as not specifying any named parameter at all).

Examples:

say ('hello', 1, 22/7, 42, 'world').grep: Int, :k;
# OUTPUT: «(1 3)␤»
say grep { .Str.chars > 3 }, :kv, 'hello', 1, 22/7, 42, 'world';
# OUTPUT: «(0 hello 2 3.142857 4 world)␤»
say grep { .Str.chars > 3 }, :p, 'hello', 1, 22/7, 42, 'world';
# OUTPUT: «(0 => hello 2 => 3.142857 4 => world)␤»

In RaceSeq§

See primary documentation in context for method grep

method grep(RaceSeq:D: $matcher, *%options)

Applies grep to the RaceSeq similarly to how it would do it on a Seq.

my @raced = (^10000).map(*²).race;
@raced.grep( * %% 3 ).say;
# OUTPUT: «(0 9 36 81 144 ...)␤»

When you use race on a Seq, this is the method that is actually called.

In HyperSeq§

See primary documentation in context for method grep

method grep(HyperSeq:D: $matcher, *%options)

Applies grep to the HyperSeq similarly to how it would do it on a Seq.

my @hyped = (^10000).map(*²).hyper;
@hyped.grep( * %% 3 ).say;
# OUTPUT: «(0 9 36 81 144 ...)␤»

When you use hyper on a Seq, this is the method that is actually called.

In Any§

See primary documentation in context for method grep

method grep(Mu $matcher, :$k, :$kv, :$p, :$v --> Seq)

Coerces the invocant to a list by applying its .list method and uses List.grep on it.

For undefined invocants, based on $matcher the return value can be either ((Any)) or the empty List.

my $a;
say $a.grep({ True }); # OUTPUT: «((Any))␤»
say $a.grep({ $_ });   # OUTPUT: «()␤»

In Supply§

See primary documentation in context for method grep

method grep(Supply:D: Mu $test --> Supply:D)

Creates a new supply that only emits those values from the original supply that smartmatch against $test.

my $supplier = Supplier.new;
my $all      = $supplier.Supply;
my $ints     = $all.grep(Int);
$ints.tap(&say);
$supplier.emit($_) for 1, 'a string', 3.14159;   # prints only 1