diff --git a/README.md b/README.md index 6da6112..2168e10 100755 --- a/README.md +++ b/README.md @@ -1,34 +1,34 @@ # Munus PHP website This website is built using Docusaurus 2, a modern static website generator. -All commit to master are automaticcly deployed to live with [buddy.works](https://buddy.works) +All commit to master are automatically deployed to live with [buddy.works](https://buddy.works) -### Installation +## Installation -``` -$ yarn +```bash +yarn ``` -### Local Development +## Local Development -``` -$ yarn start +```bash +yarn start ``` This command starts a local development server and open up a browser window. Most changes are reflected live without having to restart the server. -### Build +## Build -``` -$ yarn build +```bash +yarn build ``` This command generates static content into the `build` directory and can be served using any static contents hosting service. -### Deployment +## Deployment -``` -$ GIT_USER= USE_SSH=1 yarn deploy +```bash +GIT_USER= USE_SSH=1 yarn deploy ``` If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. diff --git a/docs/tuple.md b/docs/tuple.md index e4b3ae0..361a014 100644 --- a/docs/tuple.md +++ b/docs/tuple.md @@ -22,7 +22,7 @@ $tuple = Tuple::of('currentTime', new \DateTimeImmutable()); $tuple = Tuple::of(4, 2); $tuple[0]; // holds 4 $tuple[1]; // holds 2 -``` +``` Tuples in Munus are immutable. This means that the assignment instruction will end with an exception: @@ -31,12 +31,57 @@ $tuple = Tuple::of('Munus', 'is', 'awesome'); $tuple[1] = 'is not'; // UnsupportedOperationException will be thrown ``` +## Typing + +In Munus, Tuple implementations come with a specified size: + +```php +$tuple = Tuple::of('Munus', 'is', 'awesome'); +get_class($tuple); // gives Tuple3 +``` + +This approach provides better typing for tuples. You can use generic types to specify the type of each value: + +```php +/** @var $tuple Tuple2 */ +$tuple = Tuple::of('currentTime', new \DateTimeImmutable()); +``` + +It also simplifies the validation when you need to ensure you're working with the correct Tuple size. +Instead of doing this: + +```php +function process(Tuple $tuple): void +{ + if (!in_array($tuple->arity(), [2, 3])) { + // throw exception + } + + // processing code +} +``` + +You can do this: + +```php +function process(Tuple2|Tuple3 $tuple): void +{ + // processing code +} +``` + +:::info + +The maximum size of a `Tuple` is specified in Tuple::TUPLE_MAX_SIZE constant. + +::: + ## Methods - - `arity(): int` - returns the number of elements in this tuple - - `toArray(): array` - converts tuple to php classic array - - `append($value): self` - appends new $value to the end and returns new Tuple - - `concat(self $tuple): self` - merge one tuple with other tuple and returns new one - - `apply(callable $transformer)` - transforms tuple with given $transformer, transformer will receive array in the argument - - `map(callable $mapper): self` - maps tuple to other tuple - - `equals(self $tuple): bool` - return true if given tuple contains identical values in identical order +- `arity(): int` - returns the number of elements in this tuple +- `toArray(): array` - converts tuple to php classic array +- `append($value): self` - appends new $value to the end and returns new Tuple +- `concat(self $tuple): self` - merge one tuple with other tuple and returns new one +- `apply(callable $transformer)` - transforms tuple with given $transformer, transformer will receive array in the argument +- `map(callable $mapper): self` - maps tuple to other tuple +- `equals(self $tuple): bool` - return true if given tuple contains identical values in identical order