Documentation for routine grep
assembled from the following pages:
Class: Any §
From Any
(Any) method grep §
Defined as:
method grep(Mu , :, :, :, : --> 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 ;say .grep(); # OUTPUT: «((Any))» say .grep(); # OUTPUT: «()»
Class: Supply §
From Supply
(Supply) method grep §
method grep(Supply: Mu --> Supply)
Creates a new supply that only emits those values from the original supply that smartmatch against $test
.
my = Supplier.new;my = .Supply;my = .grep(Int);.tap();.emit() for 1, 'a string', 3.14159; # prints only 1
Class: List §
From List
(List) routine grep §
Defined as:
sub grep(Mu , *, :, :, :, : --> Seq)method grep(List: Mu , :, :, :, : --> Seq)
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 , '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() # 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 , :kv, 'hello', 1, 22/7, 42, 'world';# OUTPUT: «(0 hello 2 3.142857 4 world)» say grep , :p, 'hello', 1, 22/7, 42, 'world';# OUTPUT: «(0 => hello 2 => 3.142857 4 => world)»
Class: HyperSeq §
From HyperSeq
(HyperSeq) method grep §
method grep(HyperSeq: , *)
Applies grep
to the HyperSeq
similarly to how it would do it on a Seq
.
my = (^10000).map(*²).hyper;.grep( * %% 3 ).say;# OUTPUT: «(0 9 36 81 144 ...)»
When you use hyper
on a Seq
, this is the method that is actually called.
Class: RaceSeq §
From RaceSeq
(RaceSeq) method grep §
method grep(RaceSeq: , *)
Applies grep
to the RaceSeq
similarly to how it would do it on a Seq
.
my = (^10000).map(*²).race;.grep( * %% 3 ).say;# OUTPUT: «(0 9 36 81 144 ...)»
When you use race
on a Seq
, this is the method that is actually called.