Skip to content

Commit 7264fc4

Browse files
committed
mapping functions receive value before key as parameter
1 parent b9ea524 commit 7264fc4

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/Types/AbstractCypherSequence.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ final public function hasValue($value): bool
116116
/**
117117
* Creates a filtered the sequence with the provided callback.
118118
*
119-
* @param pure-callable(TKey, TValue):bool $callback
119+
* @param pure-callable(TValue, TKey):bool $callback
120120
*
121121
* @return static
122122
*/
123123
final public function filter(callable $callback): self
124124
{
125125
$tbr = [];
126126
foreach ($this->sequence as $key => $value) {
127-
if ($callback($key, $value)) {
127+
if ($callback($value, $key)) {
128128
$tbr[$key] = $value;
129129
}
130130
}
@@ -137,15 +137,15 @@ final public function filter(callable $callback): self
137137
*
138138
* @template U
139139
*
140-
* @param pure-callable(TKey, TValue):U $callback
140+
* @param pure-callable(TValue, TKey):U $callback
141141
*
142142
* @return static
143143
*/
144144
final public function map(callable $callback): self
145145
{
146146
$tbr = [];
147147
foreach ($this->sequence as $key => $value) {
148-
$tbr[$key] = $callback($key, $value);
148+
$tbr[$key] = $callback($value, $key);
149149
}
150150

151151
return $this::fromIterable($tbr);
@@ -156,15 +156,15 @@ final public function map(callable $callback): self
156156
*
157157
* @template TInitial
158158
*
159-
* @param pure-callable(TInitial|null, TKey, TValue):TInitial $callback
159+
* @param pure-callable(TInitial|null, TValue, TKey):TInitial $callback
160160
* @param TInitial|null $initial
161161
*
162162
* @return TInitial
163163
*/
164164
final public function reduce(callable $callback, $initial = null)
165165
{
166166
foreach ($this->sequence as $key => $value) {
167-
$initial = $callback($initial, $key, $value);
167+
$initial = $callback($initial, $value, $key);
168168
}
169169

170170
return $initial;

tests/Unit/CypherListTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
/*
45
* This file is part of the Laudis Neo4j package.
@@ -130,21 +131,21 @@ public function testFilterBlock(): void
130131

131132
public function testFilterSelective(): void
132133
{
133-
$filter = $this->list->filter(static fn (int $i, string $x) => $x === 'B' || $i === 2);
134+
$filter = $this->list->filter(static fn (string $x, int $i) => $x === 'B' || $i === 2);
134135

135136
self::assertEquals(new CypherList(['B', 'C']), $filter);
136137
}
137138

138139
public function testMap(): void
139140
{
140-
$filter = $this->list->map(static fn (int $i, string $x) => $i.':'.$x);
141+
$filter = $this->list->map(static fn (string $x, int $i) => $i.':'.$x);
141142

142143
self::assertEquals(new CypherList(['0:A', '1:B', '2:C']), $filter);
143144
}
144145

145146
public function testReduce(): void
146147
{
147-
$count = $this->list->reduce(static function (?int $initial, int $key, string $value) {
148+
$count = $this->list->reduce(static function (?int $initial, string $value, int $key) {
148149
return ($initial ?? 0) + $key * hexdec($value);
149150
}, 5);
150151

0 commit comments

Comments
 (0)