Skip to content

Commit 428db1e

Browse files
authored
Merge pull request #17 from sergeysrabionyan/reduce-modification
Reduce modification with initial value param
2 parents 5bd078b + f72592e commit 428db1e

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

src/WS/Utils/Collections/DummyStreamDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public function reverse(): Stream
108108
return $this;
109109
}
110110

111-
public function reduce(callable $accumulator)
111+
public function reduce(callable $accumulator, $initialValue = null)
112112
{
113-
return $this->decoratedStream->reduce($accumulator);
113+
return $this->decoratedStream->reduce($accumulator, $initialValue);
114114
}
115115

116116
public function limit(int $size): Stream

src/WS/Utils/Collections/SerialStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ public function max(callable $comparator)
262262
/**
263263
* @inheritDoc
264264
*/
265-
public function reduce(callable $accumulator)
265+
public function reduce(callable $accumulator, $initialValue = null)
266266
{
267-
$accumulate = null;
267+
$accumulate = $initialValue;
268268
foreach ($this->list as $item) {
269269
$accumulate = $accumulator($item, $accumulate);
270270
}

src/WS/Utils/Collections/Stream.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ public function sortByDesc(callable $extractor): Stream;
126126
* @return Stream
127127
*/
128128
public function reverse(): Stream;
129-
129+
130130
/**
131131
* Reduce collection to single value with accumulator
132-
* @param callable $accumulator
132+
* @param callable $accumulator
133+
* @param null $initialValue
133134
* @return mixed
134135
*/
135-
public function reduce(callable $accumulator);
136+
public function reduce(callable $accumulator, $initialValue = null);
136137

137138
/**
138139
* Limits amount of stream collection elements

tests/WS/Utils/Collections/SerialStreamTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ private static function fSumAggregator(): callable
3333
};
3434
}
3535

36+
private static function fEmptyFunction(): callable
37+
{
38+
return static function () {};
39+
}
40+
3641
public function createCollection(array $els): Collection
3742
{
3843
return new ArrayList($els);
@@ -207,9 +212,10 @@ public function mapConvertingChecking($input, $modifier, $expected): void
207212
public function reduceCases(): array
208213
{
209214
return [
210-
[[1,2,3], self::fSumAggregator(), 6],
211-
[[1, 2, 4], self::fCountAggregator(), 3],
212-
[[], self::fSumAggregator(), 0]
215+
[[1,2,3], self::fSumAggregator(), 4, 10],
216+
[[1, 2, 4], self::fCountAggregator(), 0, 3],
217+
[[], self::fSumAggregator(), null, null],
218+
[[], self::fEmptyFunction(), [], []]
213219
];
214220
}
215221

@@ -218,15 +224,16 @@ public function reduceCases(): array
218224
* @test
219225
* @param $input
220226
* @param $accumulator
227+
* @param $initialValue
221228
* @param $expected
222229
*/
223-
public function reduceChecking($input, $accumulator, $expected): void
230+
public function reduceChecking($input, $accumulator, $initialValue, $expected): void
224231
{
225232
$actual = $this->createCollection($input)
226233
->stream()
227-
->reduce($accumulator);
234+
->reduce($accumulator, $initialValue);
228235

229-
$this->assertEquals($expected, $actual);
236+
$this->assertSame($expected, $actual);
230237
}
231238

232239
public function aggregateCases(): array

0 commit comments

Comments
 (0)