Skip to content

Commit 3c4eb76

Browse files
authored
implement indexOf for Sequence (#96)
* implement indexOf for Sequence * fix phpstan
1 parent 1f4f6ef commit 3c4eb76

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

phpstan.neon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,3 @@ parameters:
2727
count: 1
2828
path: src/Value.php
2929

30-
-
31-
message: '# is marked as impure but does not have any side effects.#'
32-
count: 1
33-
path: src/Collection/Iterator/EmptyIterator.php

src/Collection/Sequence.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Munus\Collection;
66

7+
use Munus\Value\Comparator;
8+
79
/**
810
* Sequence - immutable sequential data structures.
911
*
@@ -40,4 +42,24 @@ abstract public function prepend($element);
4042
* @return self<T>
4143
*/
4244
abstract public function prependAll(Traversable $elements);
45+
46+
/**
47+
* Returns the index of the first occurrence of the given element or -1 if this does not contain the given element.
48+
*
49+
* @param T $element
50+
*/
51+
final public function indexOf($element): int
52+
{
53+
$sequence = $this;
54+
$index = 0;
55+
while (!$sequence->isEmpty()) {
56+
if (Comparator::equals($element, $sequence->head())) {
57+
return $index;
58+
}
59+
++$index;
60+
$sequence = $sequence->tail();
61+
}
62+
63+
return -1;
64+
}
4365
}

tests/Collection/GenericListTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Munus\Collection\Stream\Collectors;
1111
use Munus\Collection\Traversable;
1212
use Munus\Control\Option;
13+
use Munus\Tests\Stub\Event;
1314
use PHPUnit\Framework\TestCase;
1415

1516
final class GenericListTest extends TestCase
@@ -232,4 +233,27 @@ public function testFlatMap(): void
232233
GenericList::ofAll([1, 2, 3])
233234
));
234235
}
236+
237+
public function testIndexOf(): void
238+
{
239+
$list = GenericList::of('a', 'b', 'c', 'd', 'e', 'f');
240+
241+
self::assertSame(0, $list->indexOf('a'));
242+
self::assertSame(1, $list->indexOf('b'));
243+
self::assertSame(5, $list->indexOf('f'));
244+
self::assertSame(-1, $list->indexOf('g'));
245+
}
246+
247+
public function testIndexOfIsUsingComparator(): void
248+
{
249+
$list = GenericList::of(new Event('1', 'payment.failed'), new Event('2', 'payment.pending'));
250+
251+
self::assertSame(1, $list->indexOf(new Event('3', 'payment.pending')));
252+
self::assertSame(-1, $list->indexOf(new Event('1', 'payment.success')));
253+
}
254+
255+
public function testIndexOfOnEmptyList(): void
256+
{
257+
self::assertSame(-1, GenericList::empty()->indexOf('a'));
258+
}
235259
}

tests/Collection/StreamTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Munus\Collection\Traversable;
1010
use Munus\Control\Option;
1111
use Munus\Lazy;
12+
use Munus\Tests\Stub\Event;
1213
use PHPUnit\Framework\TestCase;
1314

1415
final class StreamTest extends TestCase
@@ -282,4 +283,27 @@ public function testFlatMap(): void
282283
Stream::ofAll([1, 2, 3])
283284
));
284285
}
286+
287+
public function testIndexOf(): void
288+
{
289+
$stream = Stream::of('a', 'b', 'c', 'd', 'e', 'f');
290+
291+
self::assertSame(0, $stream->indexOf('a'));
292+
self::assertSame(1, $stream->indexOf('b'));
293+
self::assertSame(5, $stream->indexOf('f'));
294+
self::assertSame(-1, $stream->indexOf('g'));
295+
}
296+
297+
public function testIndexOfIsUsingComparator(): void
298+
{
299+
$stream = Stream::of(new Event('1', 'payment.failed'), new Event('2', 'payment.pending'));
300+
301+
self::assertSame(1, $stream->indexOf(new Event('3', 'payment.pending')));
302+
self::assertSame(-1, $stream->indexOf(new Event('1', 'payment.success')));
303+
}
304+
305+
public function testIndexOfOnEmptyStream(): void
306+
{
307+
self::assertSame(-1, Stream::empty()->indexOf('a'));
308+
}
285309
}

0 commit comments

Comments
 (0)