txt: ## grep.md
  grep
  
  Combined from primary sources listed below.
  
  # [In Any](#___top "go to top of document")[§](#(Any)_method_grep "direct link")
  
  See primary documentation
  [in context](/type/Any#method_grep)
  for **method grep**.
  
  ```raku
  method grep(Mu $matcher, :$k, :$kv, :$p, :$v --> Seq)
  ```
  
  Coerces the invocant to a `list` by applying its [`.list`](/routine/list) method and uses [`List.grep`](/type/List#routine_grep) on it.
  
  For undefined invocants, based on `$matcher` the return value can be either `((Any))` or the empty List.
  
  ```raku
  my $a;
  say $a.grep({ True }); # OUTPUT: «((Any))␤»
  say $a.grep({ $_ });   # OUTPUT: «()␤»
  ```
  
  # [In HyperSeq](#___top "go to top of document")[§](#(HyperSeq)_method_grep "direct link")
  
  See primary documentation
  [in context](/type/HyperSeq#method_grep)
  for **method grep**.
  
  ```raku
  method grep(HyperSeq:D: $matcher, *%options)
  ```
  
  Applies `grep` to the `HyperSeq` similarly to how it would do it on a [`Seq`](/type/Seq).
  
  ```raku
  my @hyped = (^10000).map(*²).hyper;
  @hyped.grep( * %% 3 ).say;
  # OUTPUT: «(0 9 36 81 144 ...)␤»
  
  ```
  
  When you use `hyper` on a [`Seq`](/type/Seq), this is the method that is actually called.
  
  # [In List](#___top "go to top of document")[§](#(List)_routine_grep "direct link")
  
  See primary documentation
  [in context](/type/List#routine_grep)
  for **routine grep**.
  
  ```raku
  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:
  
  ```raku
  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`](/type/Junction):
  
  ```raku
  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:
  
  ```raku
  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`](/type/Callable) (in this case a [`Block`](/type/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`](/type/Pair), in order.
  
  * v
  
  Only return the matched elements (same as not specifying any named parameter at all).
  
  Examples:
  
  ```raku
  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 Supply](#___top "go to top of document")[§](#(Supply)_method_grep "direct link")
  
  See primary documentation
  [in context](/type/Supply#method_grep)
  for **method grep**.
  
  ```raku
  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`.
  
  ```raku
  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
  ```
  
  # [In RaceSeq](#___top "go to top of document")[§](#(RaceSeq)_method_grep "direct link")
  
  See primary documentation
  [in context](/type/RaceSeq#method_grep)
  for **method grep**.
  
  ```raku
  method grep(RaceSeq:D: $matcher, *%options)
  ```
  
  Applies `grep` to the `RaceSeq` similarly to how it would do it on a [`Seq`](/type/Seq).
  
  ```raku
  my @raced = (^10000).map(*²).race;
  @raced.grep( * %% 3 ).say;
  # OUTPUT: «(0 9 36 81 144 ...)␤»
  
  ```
  
  When you use `race` on a [`Seq`](/type/Seq), this is the method that is actually called.
  