Skip to content

Instantly share code, notes, and snippets.

@haampie
Last active September 4, 2023 14:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save haampie/474bcdc4a6de09e8eceafc82712e5607 to your computer and use it in GitHub Desktop.
Save haampie/474bcdc4a6de09e8eceafc82712e5607 to your computer and use it in GitHub Desktop.
Collections
<?php
final class User
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
}
class Collection extends IteratorIterator
{
public function take(int $n): Collection
{
return new static(new LimitIterator($this, 0, $n));
}
public function filter(callable $f): Collection
{
return new static(new CallbackFilterIterator($this, $f));
}
public function get(): array
{
return iterator_to_array($this);
}
}
final class UsersCollection extends Collection
{
public function __construct(Traversable $traversable)
{
// Can't make the constructor private :[
parent::__construct($traversable);
}
public static function safe(User ...$users)
{
return new self(new ArrayIterator($users));
}
public static function unsafe(Traversable $traversable)
{
return new self($traversable);
}
public function current(): User
{
return parent::current();
}
}
/**
* Example 1 shows how to do lazy stuff
*/
function example1()
{
$users = UsersCollection::unsafe((function () {
while (true) {
yield new User(str_shuffle('ABCDEFGH'));
}
})());
$filter = function (User $user) {
return $user->getName()[0] == 'H';
};
$view = $users->filter($filter)->take(10);
echo get_class($view) . PHP_EOL;
foreach ($view as $user) {
echo $user->getName() . PHP_EOL;
}
}
/**
* Example 2 shows how to do eager stuff
*/
function example2()
{
$users = UsersCollection::safe(new User('Foo'), new User('Bar'), new User('Baz'));
foreach ($users->take(2) as $user) {
echo $user->getName() . PHP_EOL;
}
}
/**
* Retrieving the underlying array.
*/
function example3()
{
$users = UsersCollection::unsafe((function () {
while (true) {
yield new User(str_shuffle('ABCDEFGH'));
}
})());
$ten = $users->take(10)->get();
$transformed = UsersCollection::safe(...array_map(function (User $user): User {
return new User("Good guy " . $user->getName());
}, $ten));
foreach ($transformed as $name) {
echo $name->getName() . PHP_EOL;
}
}
echo "Example 1: eager loading\n";
example1();
echo "\nExample 2: lazy loading\n";
example2();
echo "\nExample 3: delegate map functions and the like\n";
example3();
Example 1: eager loading
UsersCollection
HBCFGAED
HEGBDCFA
HBFGCDEA
HBFCGADE
HDGEABCF
HFGCDABE
HBCEGFDA
HEADBCGF
HGCFDEBA
HFCDGEBA
Example 2: lazy loading
Foo
Bar
Example 3: delegate map functions and the like
Good guy BDEGHCFA
Good guy BEFCGHDA
Good guy ABCEGHDF
Good guy EBCGAHDF
Good guy FEHDCAGB
Good guy CAFBHEGD
Good guy AEFHGDCB
Good guy DCHFAEGB
Good guy FABGCEHD
Good guy CEHFDGAB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment