| @@ 10-21 (lines=12) @@ | ||
| 7 | * @param string|callable $fn '$_ > 100' |
|
| 8 | * @return $this |
|
| 9 | */ |
|
| 10 | public function filter($fn) |
|
| 11 | { |
|
| 12 | $this->is_array = false; |
|
| 13 | if (is_callable($fn)) { |
|
| 14 | $fn_name = '_fn' . $this->fn_cnt++; |
|
| 15 | $this->vars[$fn_name] = $fn; |
|
| 16 | $this->ops[] = ' if (!$' . $fn_name . '($_)) continue;'; |
|
| 17 | } else { |
|
| 18 | $this->ops[] = ' if (!(' . $fn . ')) continue;'; |
|
| 19 | } |
|
| 20 | return $this->step(); |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param string|callable $fn '$_ > 100' |
|
| @@ 27-38 (lines=12) @@ | ||
| 24 | * @param string|callable $fn '$_ > 100' |
|
| 25 | * @return $this |
|
| 26 | */ |
|
| 27 | public function reject($fn) |
|
| 28 | { |
|
| 29 | $this->is_array = false; |
|
| 30 | if (is_callable($fn)) { |
|
| 31 | $fn_name = '_fn' . $this->fn_cnt++; |
|
| 32 | $this->vars[$fn_name] = $fn; |
|
| 33 | $this->ops[] = ' if ($' . $fn_name . '($_)) continue;'; |
|
| 34 | } else { |
|
| 35 | $this->ops[] = ' if (' . $fn . ') continue;'; |
|
| 36 | } |
|
| 37 | return $this->step(); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @param int $offset |
|
| @@ 10-21 (lines=12) @@ | ||
| 7 | * @param string|callable $fn ($val) => $val |
|
| 8 | * @return \Spindle\Collection\Collection |
|
| 9 | */ |
|
| 10 | public function map($fn) |
|
| 11 | { |
|
| 12 | $this->is_array = false; |
|
| 13 | if (is_callable($fn)) { |
|
| 14 | $fn_name = '_fn' . $this->fn_cnt++; |
|
| 15 | $this->vars[$fn_name] = $fn; |
|
| 16 | $this->ops[] = ' $_ = $' . $fn_name . '($_);'; |
|
| 17 | } else { |
|
| 18 | $this->ops[] = ' $_ = ' . $fn . ';'; |
|
| 19 | } |
|
| 20 | return $this->step(); |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @param string|callable $fn ($key, $val) => $val |
|
| @@ 27-38 (lines=12) @@ | ||
| 24 | * @param string|callable $fn ($key, $val) => $val |
|
| 25 | * @return \Spindle\Collection\Collection |
|
| 26 | */ |
|
| 27 | public function mapWithKey($fn) |
|
| 28 | { |
|
| 29 | $this->is_array = false; |
|
| 30 | if (is_callable($fn)) { |
|
| 31 | $fn_name = '_fn' . $this->fn_cnt++; |
|
| 32 | $this->vars[$fn_name] = $fn; |
|
| 33 | $this->ops[] = ' $_ = $' . $fn_name . '($_key, $_);'; |
|
| 34 | } else { |
|
| 35 | $this->ops[] = ' $_ = ' . $fn . ';'; |
|
| 36 | } |
|
| 37 | return $this->step(); |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @param string|string[] columns |
|