Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorted method for traversable values #81

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Collection/GenericList.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public function filter(callable $predicate)
return $filtered->reverse();
}

/**
* @return GenericList<T>
*/
public function sorted()
{
return self::ofAll($this->iterator()->sort());
}

/**
* @param callable(T):bool $predicate
*
Expand Down
8 changes: 8 additions & 0 deletions src/Collection/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,12 @@ public function fold($zero, callable $combine)

return $zero;
}

public function sort(): self
{
$array = $this->toArray();
asort($array);

return self::of(...$array);
}
}
11 changes: 11 additions & 0 deletions src/Collection/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ public function filter(callable $predicate)
return self::fromPointer($map);
}

/**
* @return Map<string,V>
*/
public function sorted()
{
$map = $this->map;
asort($map);

return self::fromPointer($map);
}

/**
* @param callable(Tuple):bool $predicate
*
Expand Down
11 changes: 11 additions & 0 deletions src/Collection/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ public function filter(callable $predicate)
return self::fromPointer($filtered);
}

/**
* @return Set<T>
*/
public function sorted()
{
$elements = $this->elements;
asort($elements);

return self::fromPointer($elements);
}

/**
* @param callable(T):bool $predicate
*
Expand Down
8 changes: 8 additions & 0 deletions src/Collection/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ public function filter(callable $predicate)
});
}

/**
* @return Stream<T>
*/
public function sorted()
{
return self::ofAll($this->getIterator()->sort());
}

/**
* @param callable(T):bool $predicate
*
Expand Down
5 changes: 5 additions & 0 deletions src/Collection/Traversable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ abstract public function map(callable $mapper);
*/
abstract public function filter(callable $predicate);

/**
* @return Traversable<T>
*/
abstract public function sorted();

/**
* @return Traversable<T>
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Collection/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,9 @@ public function testListAppendAll(): void
self::assertTrue(GenericList::of(1, 2, 3, 4)->equals(GenericList::of(1, 2)->appendAll(GenericList::of(3, 4))));
self::assertTrue(GenericList::of('a', 'b', 'c', 'd', 'e')->equals(GenericList::of('a')->appendAll(GenericList::of('b', 'c', 'd', 'e'))));
}

public function testListSorted(): void
{
self::assertTrue(GenericList::of('a', 'b', 'c', 'd', 'e')->equals(GenericList::of('e', 'd', 'c', 'b', 'a')->sorted()));
}
}
5 changes: 5 additions & 0 deletions tests/Collection/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,9 @@ public function testMapToArray(): void
{
self::assertEquals(['a' => 'b', 'c' => 'd'], Map::fromArray(['a' => 'b', 'c' => 'd'])->toArray());
}

public function testMapSorted(): void
{
self::assertTrue(Map::fromArray(['a' => 'b', 'c' => 'd', 'e' => 'f'])->equals(Map::fromArray(['e' => 'f', 'c' => 'd', 'a' => 'b'])->sorted()));
}
}
5 changes: 5 additions & 0 deletions tests/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,9 @@ public function testToArray(): void
{
self::assertEquals(['php', 'is', 'awesome'], Set::of('php', 'is', 'awesome')->toArray());
}

public function testSorted(): void
{
self::assertTrue(Set::of('a', 'b', 'c', 'd', 'e')->equals(Set::of('e', 'd', 'c', 'b', 'a')->sorted()));
}
}
5 changes: 5 additions & 0 deletions tests/Collection/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,9 @@ public function testStreamToArray(): void
{
self::assertEquals([1, 2, 3, 4, 5], Stream::range(1, 5)->toArray());
}

public function testStreamSorted(): void
{
self::assertTrue(Stream::of('a', 'b', 'c', 'd', 'e')->equals(Stream::of('e', 'd', 'c', 'b', 'a')->sorted()));
}
}
Loading