Skip to content

Commit 18e353b

Browse files
committed
Functions added:
- `map()` - `filter()` - `reduce()`
1 parent c27fc8e commit 18e353b

3 files changed

Lines changed: 373 additions & 12 deletions

File tree

README.md

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ use function Smoren\Sequence\Functions\xrange;
3030
foreach(xrange(5) as $i) { // start: 0; count: 5; step: 1
3131
echo "{$i} ";
3232
}
33-
// out: 0 1 2 3 4
33+
// 0 1 2 3 4
3434

3535
foreach(xrange(1, 5) as $i) { // start: 1; count: 5; step: 1
3636
echo "{$i} ";
3737
}
38-
// out: 1 2 3 4 5
38+
// 1 2 3 4 5
3939

4040
foreach(xrange(1, 5, 2) as $i) { // start: 1; count: 5; step: 2
4141
echo "{$i} ";
4242
}
43-
// out: 1 3 5 7 9
43+
// 1 3 5 7 9
4444
```
4545

4646
#### Range
@@ -56,7 +56,7 @@ var_dump($range->isInfinite()); // false
5656
foreach($range as $value) {
5757
echo "{$value} ";
5858
}
59-
// out: 1 3 5
59+
// 1 3 5
6060

6161
var_dump($range[0]); // 1
6262
var_dump($range[1]); // 3
@@ -86,7 +86,7 @@ foreach($range as $i => $value) {
8686
echo "{$value} ";
8787
if($i > 100) break;
8888
}
89-
// out: 1 3 5 7 9 11 13...
89+
// 1 3 5 7 9 11 13...
9090

9191
/* Float range */
9292
$range = new Range(1.1, 3, 2.1);
@@ -95,7 +95,7 @@ var_dump($range->isInfinite()); // false
9595
foreach($range as $value) {
9696
echo "{$value} ";
9797
}
98-
// out: 1.1 3.2 5.3
98+
// 1.1 3.2 5.3
9999
```
100100

101101
#### Exponential
@@ -111,7 +111,7 @@ var_dump($sequence->isInfinite()); // false
111111
foreach($sequence as $value) {
112112
echo "{$value} ";
113113
}
114-
// out: 1 2 4 8
114+
// 1 2 4 8
115115

116116
var_dump($sequence[0]); // 1
117117
var_dump($sequence[1]); // 2
@@ -143,7 +143,7 @@ foreach($sequence as $i => $value) {
143143
echo "{$value} ";
144144
if($i > 100) break;
145145
}
146-
// out: 1 2 4 8 16 32 64...
146+
// 1 2 4 8 16 32 64...
147147

148148
/* Infinite float exponential sequence */
149149
$sequence = new Exponential(0.5, null, 2);
@@ -152,7 +152,7 @@ var_dump($sequence->isInfinite()); // true
152152
foreach($sequence as $value) {
153153
echo "{$value} ";
154154
}
155-
// out: 0.5 0.25 0.125...
155+
// 0.5 0.25 0.125...
156156
```
157157

158158
#### IndexedArray
@@ -193,3 +193,76 @@ try {
193193
echo "cannot unset value from index out of range\n";
194194
}
195195
```
196+
197+
#### xrange
198+
199+
Function for creating iterable range.
200+
201+
```xrange(int $start, ?int $size = null, int $step = 1): Range```
202+
203+
```php
204+
use function Smoren\Sequence\Functions\xrange;
205+
206+
$range = xrange(5);
207+
print_r(iterator_to_array($range));
208+
// [0, 1, 2, 3, 4]
209+
210+
$range = xrange(1, 5);
211+
print_r(iterator_to_array($range));
212+
// [1, 2, 3, 4, 5]
213+
214+
$range = xrange(1, 5, 2);
215+
print_r(iterator_to_array($range));
216+
// [1, 3, 5, 7, 9]
217+
```
218+
219+
#### map
220+
221+
Function for mapping iterable collection and creating IndexedArray of mapped values as a result.
222+
223+
```map(iterable $collection, callable $mapper): IndexedArray```
224+
225+
```php
226+
use function Smoren\Sequence\Functions\map;
227+
228+
$input = [1, 2, 3, 4, 5];
229+
$result = map($input, static function($item) {
230+
return $item + 2;
231+
});
232+
print_r($result->toArray());
233+
// [3, 4, 5, 6, 7]
234+
```
235+
236+
#### filter
237+
238+
Function for filtering iterable collection and returning IndexedArray of filtered items.
239+
240+
```filter(iterable $collection, callable $filter): IndexedArray```
241+
242+
```php
243+
use function Smoren\Sequence\Functions\filter;
244+
245+
$input = [1, 2, 3, 4, 5];
246+
$result = filter($input, static function($item) {
247+
return $item > 2;
248+
});
249+
print_r($result->toArray());
250+
// [3, 4, 5]
251+
```
252+
253+
#### reduce
254+
255+
Function for reduction of iterable collection.
256+
257+
```reduce(iterable $collection, callable $reducer, mixed $initialValue = null): IndexedArray```
258+
259+
```php
260+
use function Smoren\Sequence\Functions\reduce;
261+
262+
$input = [1, 2, 3, 4, 5];
263+
$result = filter($input, static function($carry, $item) {
264+
return $carry + $item;
265+
}, 0);
266+
var_dump($result);
267+
// 15
268+
```

src/functions.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Smoren\Sequence\Functions;
44

5+
use Smoren\Sequence\Structs\IndexedArray;
56
use Smoren\Sequence\Structs\Range;
67

78
/**
89
* Creates iterable range.
910
*
10-
* If size is null then range is infinite.
11-
*
1211
* @param int $start start value
13-
* @param int<0, max>|null $size size of elements (infinite if null)
12+
* @param int<0, max>|null $size size of elements
1413
* @param int $step range step
1514
*
1615
* @return Range<int> iterable range
@@ -23,3 +22,71 @@ function xrange(int $start, ?int $size = null, int $step = 1): Range
2322

2423
return new Range($start, $size, $step);
2524
}
25+
26+
/**
27+
* Maps iterable collection and returns IndexedArray of mapped values.
28+
*
29+
* @template TInput
30+
* @template TOutput
31+
*
32+
* @param iterable<TInput> $collection
33+
* @param callable(TInput $item): TOutput $mapper
34+
*
35+
* @return IndexedArray<TOutput>
36+
*/
37+
function map(iterable $collection, callable $mapper): IndexedArray
38+
{
39+
$result = new IndexedArray();
40+
41+
foreach($collection as $item) {
42+
$result[] = $mapper($item);
43+
}
44+
45+
return $result;
46+
}
47+
48+
/**
49+
* Filters iterable collection and returns IndexedArray of filtered items.
50+
*
51+
* @template T
52+
*
53+
* @param iterable<T> $collection
54+
* @param callable(T $item): T $filter
55+
*
56+
* @return IndexedArray<T>
57+
*/
58+
function filter(iterable $collection, callable $filter): IndexedArray
59+
{
60+
$result = new IndexedArray();
61+
62+
foreach($collection as $item) {
63+
if($filter($item)) {
64+
$result[] = $item;
65+
}
66+
}
67+
68+
return $result;
69+
}
70+
71+
/**
72+
* Reduces iterable collection.
73+
*
74+
* @template TInput
75+
* @template TOutput
76+
*
77+
* @param iterable<TInput> $collection
78+
* @param callable(TOutput|null $carry, TInput $item): TOutput $reducer
79+
* @param TOutput|null $initialValue
80+
*
81+
* @return TOutput|null
82+
*/
83+
function reduce(iterable $collection, callable $reducer, $initialValue = null)
84+
{
85+
$carry = $initialValue;
86+
87+
foreach($collection as $item) {
88+
$carry = $reducer($carry, $item);
89+
}
90+
91+
return $carry;
92+
}

0 commit comments

Comments
 (0)