Skip to content

Commit 64e9205

Browse files
committed
Added sliding() method
1 parent e34a4e6 commit 64e9205

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ will return:
250250
<a href="#shuffled">shuffled</a>
251251
<a href="#skip">skip</a>
252252
<a href="#slice">slice</a>
253+
<a href="#sliding">sliding</a>
253254
<a href="#some">some</a>
254255
<a href="#sort">sort</a>
255256
<a href="#sorted">sorted</a>
@@ -488,6 +489,7 @@ will return:
488489
* [rekey()](#rekey) : Changes the keys according to the passed function
489490
* [replace()](#replace) : Replaces elements recursively
490491
* [rtrim()](#rtrim) : Removes the passed characters from the right of all strings
492+
* [sliding()](#sliding) : Returns a new map containing sliding windows of the original map
491493
* [splice()](#splice) : Replaces a slice by new elements
492494
* [strAfter()](#strafter) : Returns the strings after the passed value
493495
* [strBefore()](#strbefore) : Returns the strings before the passed value
@@ -5463,6 +5465,36 @@ Map::from( ['a', 'b', 'c', 'd'] )->slice( -2, -1 );
54635465
* [take()](#take) - Returns a new map with the given number of items.
54645466

54655467

5468+
### sliding()
5469+
5470+
Returns a new map containing sliding windows of the original map.
5471+
5472+
```php
5473+
public function sliding( int $size = 2, int $step = 1 ) : self
5474+
```
5475+
5476+
* @param **int** $size Size of each window
5477+
* @param **int** $step Step size to move the window
5478+
* @return **self&#60;int,array&#60;int&#124;string,mixed&#62;&#62;** New map containing arrays for each window
5479+
5480+
**Examples:**
5481+
5482+
```php
5483+
Map::from( [1, 2, 3, 4] )->sliding( 2 );
5484+
// [
5485+
// [0 => 1, 1 => 2],
5486+
// [1 => 2, 2 => 3],
5487+
// [2 => 3, 3 => 4]
5488+
// ]
5489+
5490+
Map::from( [1, 2, 3, 4] )->sliding( 3, 2 );
5491+
// [
5492+
// [0 => 1, 1 => 2, 2 => 3],
5493+
// [2 => 3, 3 => 4, 4 => 5]
5494+
// ]
5495+
```
5496+
5497+
54665498
### some()
54675499

54685500
Tests if at least one element passes the test or is part of the map.

src/Map.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,34 @@ public function slice( int $offset, ?int $length = null ) : self
44804480
}
44814481

44824482

4483+
/**
4484+
* Returns a new map containing sliding windows of the original map.
4485+
*
4486+
* Examples:
4487+
* Map::from( [1, 2, 3, 4] )->sliding( 2 );
4488+
* Map::from( [1, 2, 3, 4] )->sliding( 3, 2 );
4489+
*
4490+
* Results:
4491+
* The first example will return [[0 => 1, 1 => 2], [1 => 2, 2 => 3], [2 => 3, 3 => 4]]
4492+
* while the second one will return [[0 => 1, 1 => 2, 2 => 3], [2 => 3, 3 => 4, 4 => 5]]
4493+
*
4494+
* @param int $size Size of each window
4495+
* @param int $step Step size to move the window
4496+
* @return self<int,array<int|string,mixed>> New map containing arrays for each window
4497+
*/
4498+
public function sliding( int $size = 2, int $step = 1 ) : self
4499+
{
4500+
$result = [];
4501+
$chunks = floor( ( $this->count() - $size ) / $step ) + 1;
4502+
4503+
for( $i = 0; $i < $chunks; $i++ ) {
4504+
$result[] = array_slice( $this->list(), $i * $step, $size, true );
4505+
}
4506+
4507+
return new static( $result );
4508+
}
4509+
4510+
44834511
/**
44844512
* Tests if at least one element passes the test or is part of the map.
44854513
*

tests/MapTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,6 +3104,20 @@ public function testSliceNegativeOffsetAndNegativeLength()
31043104
}
31053105

31063106

3107+
public function testSliding()
3108+
{
3109+
$expected = [[0 => 1, 1 => 2], [1 => 2, 2 => 3], [2 => 3, 3 => 4], [3 => 4, 4 => 5]];
3110+
$this->assertSame( $expected, Map::from( [1, 2, 3, 4, 5] )->sliding( 2 )->toArray() );
3111+
}
3112+
3113+
3114+
public function testSlidingStep()
3115+
{
3116+
$expected = [[0 => 1, 1 => 2, 2 => 3], [2 => 3, 3 => 4, 4 => 5]];
3117+
$this->assertSame( $expected, Map::from( [1, 2, 3, 4, 5] )->sliding( 3, 2 )->toArray() );
3118+
}
3119+
3120+
31073121
public function testSome()
31083122
{
31093123
$this->assertTrue( Map::from( ['a', 'b'] )->some( 'a' ) );

0 commit comments

Comments
 (0)