original_string: protected function grep($expression, $limit = null)
      {
          // initialize our return map and book-keeping values
          $map = new static;
          $bnd = empty($limit) ? null : abs($limit);
          $cnt = 0;
  
          // define a helper to add matching values to our new map, stopping when
          // any designated limit is reached
          $helper = function ($value, $key) use ($expression, $map, $bnd, &$cnt) {
              if ($this->passes($this->call($expression, $value, $key))) {
                  $map->set($key, $value);
                  if (null !== $bnd && $bnd <= ++$cnt) {
                      return false;
                  }
              }
          };
  
          // walk the array in the right direction
          if (0 <= $limit) {
              $this->walk($helper);
  
          } else {
              $this->walk_backward($helper);
          }
  
          return $map;
      }
code: protected function grep($expression, $limit = null)
      {
          // initialize our return map and book-keeping values
          $map = new static;
          $bnd = empty($limit) ? null : abs($limit);
          $cnt = 0;
  
          // define a helper to add matching values to our new map, stopping when
          // any designated limit is reached
          $helper = function ($value, $key) use ($expression, $map, $bnd, &$cnt) {
              if ($this->passes($this->call($expression, $value, $key))) {
                  $map->set($key, $value);
                  if (null !== $bnd && $bnd <= ++$cnt) {
                      return false;
                  }
              }
          };
  
          // walk the array in the right direction
          if (0 <= $limit) {
              $this->walk($helper);
  
          } else {
              $this->walk_backward($helper);
          }
  
          return $map;
      }
code_tokens: protected function grep ( $ expression , $ limit = null ) { // initialize our return map and book-keeping values $ map = new static ; $ bnd = empty ( $ limit ) ? null : abs ( $ limit ) ; $ cnt = 0 ; // define a helper to add matching values to our new map, stopping when // any designated limit is reached $ helper = function ( $ value , $ key ) use ( $ expression , $ map , $ bnd , & $ cnt ) { if ( $ this -> passes ( $ this -> call ( $ expression , $ value , $ key ) ) ) { $ map -> set ( $ key , $ value ) ; if ( null !== $ bnd && $ bnd <= ++ $ cnt ) { return false ; } } } ; // walk the array in the right direction if ( 0 <= $ limit ) { $ this -> walk ( $ helper ) ; } else { $ this -> walk_backward ( $ helper ) ; } return $ map ; }
docstring: Finds elements for which the given code passes, optionally limited to a
  maximum count.
  
  If limit is null, no limit on number of matches. If limit is positive,
  return that many from the front of the array. If limit is negative,
  return that many from the end of the array.
  
  @param callable|string $expression
  @param int|null $limit
  @return new static
docstring_tokens: Finds elements for which the given code passes optionally limited to a maximum count .