Skip to content

Simple implementation of immutable ordered sequence of elements with strict types. Which means that once tuple created it can't be immuted. Tuple also provide elements of the same type.

Notifications You must be signed in to change notification settings

rocketfellows/tuple

Repository files navigation

Simple tuple implementation

Code Coverage Badge

This component is simple implementation of immutable ordered sequence of elements with strict types. Which means that once tuple created it can't be immuted. Tuple also provide elements of the same type.

Installation

composer require rocketfellows/tuple

Usage example

Typed item class:

class TupleItem
{
    private $id;

    public function __construct(int $id)
    {
        $this->id = $id;
    }

    public function getId(): int
    {
        return $this->id;
    }
}

Items tuple implementation:

use rocketfellows\tuple\Tuple;

class Items extends Tuple
{
    public function __construct(TupleItem ...$items)
    {
        parent::__construct(...$items);
    }

    public function current(): ?TupleItem
    {
        return parent::current();
    }
}

Load tuple and loop through:

$firstTupleItem = new TupleItem(1);
$secondTupleItem = new TupleItem(2);
$thirdTupleItem = new TupleItem(3);

$items = new Items($firstTupleItem, $secondTupleItem, $thirdTupleItem);

foreach ($items as $item) {
    print_r($item->getId());
}

Result:

1
2
3

Contributing

Welcome to pull requests. If there is a major changes, first please open an issue for discussion.

Please make sure to update tests as appropriate.

About

Simple implementation of immutable ordered sequence of elements with strict types. Which means that once tuple created it can't be immuted. Tuple also provide elements of the same type.

Topics

Resources

Stars

Watchers

Forks

Packages