Skip to content

Commit e0825c4

Browse files
committed
setup each function
1 parent 9f7f5b6 commit e0825c4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Types/AbstractCypherSequence.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,20 @@ public function join(?string $glue = null): string
212212
/** @psalm-suppress MixedArgumentTypeCoercion */
213213
return implode($glue ?? '', $this->sequence);
214214
}
215+
216+
/**
217+
* Iterates over the sequence and applies the callable.
218+
*
219+
* @param callable(TValue, TKey):void $callable
220+
*
221+
* @return static<TValue, TKey>
222+
*/
223+
public function each(callable $callable): self
224+
{
225+
foreach ($this->sequence as $key => $value) {
226+
$callable($value, $key);
227+
}
228+
229+
return $this;
230+
}
215231
}

tests/Unit/CypherListTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,15 @@ public function testSortedCustom(): void
352352
self::assertEquals(new CypherList(['A', 'B', 'C']), $this->list);
353353
}
354354

355+
public function testEach(): void
356+
{
357+
$cntr = -1;
358+
/** @psalm-suppress UnusedClosureParam */
359+
$this->list->each(static function (string $x, int $key) use (&$cntr) { $cntr = $key; });
360+
361+
self::assertEquals($this->list->count() - 1, $cntr);
362+
}
363+
355364
public function testMapTypings(): void
356365
{
357366
$map = CypherList::fromIterable(['a', 'b', 'c'])

0 commit comments

Comments
 (0)