Skip to content

Commit

Permalink
implement indexOf for Sequence (#96)
Browse files Browse the repository at this point in the history
* implement indexOf for Sequence

* fix phpstan
  • Loading branch information
akondas authored May 17, 2024
1 parent 1f4f6ef commit 3c4eb76
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,3 @@ parameters:
count: 1
path: src/Value.php

-
message: '# is marked as impure but does not have any side effects.#'
count: 1
path: src/Collection/Iterator/EmptyIterator.php
22 changes: 22 additions & 0 deletions src/Collection/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Munus\Collection;

use Munus\Value\Comparator;

/**
* Sequence - immutable sequential data structures.
*
Expand Down Expand Up @@ -40,4 +42,24 @@ abstract public function prepend($element);
* @return self<T>
*/
abstract public function prependAll(Traversable $elements);

/**
* Returns the index of the first occurrence of the given element or -1 if this does not contain the given element.
*
* @param T $element
*/
final public function indexOf($element): int
{
$sequence = $this;
$index = 0;
while (!$sequence->isEmpty()) {
if (Comparator::equals($element, $sequence->head())) {
return $index;
}
++$index;
$sequence = $sequence->tail();
}

return -1;
}
}
24 changes: 24 additions & 0 deletions tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Munus\Collection\Stream\Collectors;
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Tests\Stub\Event;
use PHPUnit\Framework\TestCase;

final class GenericListTest extends TestCase
Expand Down Expand Up @@ -232,4 +233,27 @@ public function testFlatMap(): void
GenericList::ofAll([1, 2, 3])
));
}

public function testIndexOf(): void
{
$list = GenericList::of('a', 'b', 'c', 'd', 'e', 'f');

self::assertSame(0, $list->indexOf('a'));
self::assertSame(1, $list->indexOf('b'));
self::assertSame(5, $list->indexOf('f'));
self::assertSame(-1, $list->indexOf('g'));
}

public function testIndexOfIsUsingComparator(): void
{
$list = GenericList::of(new Event('1', 'payment.failed'), new Event('2', 'payment.pending'));

self::assertSame(1, $list->indexOf(new Event('3', 'payment.pending')));
self::assertSame(-1, $list->indexOf(new Event('1', 'payment.success')));
}

public function testIndexOfOnEmptyList(): void
{
self::assertSame(-1, GenericList::empty()->indexOf('a'));
}
}
24 changes: 24 additions & 0 deletions tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Munus\Collection\Traversable;
use Munus\Control\Option;
use Munus\Lazy;
use Munus\Tests\Stub\Event;
use PHPUnit\Framework\TestCase;

final class StreamTest extends TestCase
Expand Down Expand Up @@ -282,4 +283,27 @@ public function testFlatMap(): void
Stream::ofAll([1, 2, 3])
));
}

public function testIndexOf(): void
{
$stream = Stream::of('a', 'b', 'c', 'd', 'e', 'f');

self::assertSame(0, $stream->indexOf('a'));
self::assertSame(1, $stream->indexOf('b'));
self::assertSame(5, $stream->indexOf('f'));
self::assertSame(-1, $stream->indexOf('g'));
}

public function testIndexOfIsUsingComparator(): void
{
$stream = Stream::of(new Event('1', 'payment.failed'), new Event('2', 'payment.pending'));

self::assertSame(1, $stream->indexOf(new Event('3', 'payment.pending')));
self::assertSame(-1, $stream->indexOf(new Event('1', 'payment.success')));
}

public function testIndexOfOnEmptyStream(): void
{
self::assertSame(-1, Stream::empty()->indexOf('a'));
}
}

0 comments on commit 3c4eb76

Please sign in to comment.