Skip to content

Commit

Permalink
documentation: Add examples to Result methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mathroc committed Jul 17, 2022
1 parent 66f23f4 commit b27796a
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function writeInFile(string $filepath, string $data): Result {
$res = file_put_contents($filepath, $data);

if ($res === false) {
return Result\err("failed to write in $path");
return Result\err("failed to write in $filepath");
}

return Result\ok($res);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"vimeo/psalm": "^4.22",
"symfony/finder": "^6.1",
"symfony/console": "^6.1",
"texthtml/doctest": "^0.1.0"
"texthtml/doctest": "^0.2"
},
"config": {
"allow-plugins": {
Expand Down
17 changes: 9 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 2 additions & 40 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* // @return Option<float>
* function divide(float $numerator, float $denominator): Option {
* if ($denominator === 0.0) {
Expand Down Expand Up @@ -50,15 +48,11 @@ interface Option extends \IteratorAggregate
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some("value");
* self::assertSame($x->expect("fruits are healthy"), "value");
* ```
*
* ```
* use TH\Maybe\Option;
*
* // @var Option<string> $x
* $x = Option\none();
*
Expand All @@ -78,15 +72,11 @@ public function expect(string $message): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some("value");
* self::assertSame($x->unwrap(), "value");
* ```
*
* ```
* use TH\Maybe\Option;
*
* // @var Option<string> $x
* $x = Option\none();
*
Expand All @@ -105,8 +95,6 @@ public function unwrap(): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* self::assertSame(Option\some("car")->unwrapOr("bike"), "car");
* self::assertSame(Option\none()->unwrapOr("bike"), "bike");
* ```
Expand All @@ -122,8 +110,6 @@ public function unwrapOr(mixed $default): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $k = 10;
* self::assertSame(Option\some(4)->unwrapOrElse(fn () => 2 * $k), 4);
* self::assertSame(Option\none()->unwrapOrElse(fn () => 2 * $k), 20);
Expand All @@ -141,8 +127,6 @@ public function unwrapOrElse(callable $default): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $option = Option\some(4);
* self::assertSame($option->inspect(fn (int $n) => printf("got: %d", $n)), $option); // @prints got: 4
* // @var Option<int> $option
Expand All @@ -161,8 +145,6 @@ public function inspect(callable $callback): self;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some(2);
* // @var Option<string> $y
* $y = Option\none();
Expand Down Expand Up @@ -193,8 +175,6 @@ public function and(Option $right): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* // @return Option<int>
* function to_exact_int(float $f): Option {
* $i = (int) $f;
Expand All @@ -218,8 +198,6 @@ public function andThen(callable $right): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some(2);
* // @var Option<int> $y
* $y = Option\none();
Expand Down Expand Up @@ -252,8 +230,6 @@ public function or(Option $right): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* // @return Option<string>
* function nobody(): Option {
* return Option\none();
Expand All @@ -280,8 +256,6 @@ public function orElse(callable $right): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some(2);
* // @var Option<int> $y
* $y = Option\none();
Expand Down Expand Up @@ -311,8 +285,6 @@ public function xor(Option $right): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some(2);
* self::assertTrue($x->contains(2));
* $x = Option\some(3);
Expand All @@ -332,8 +304,6 @@ public function contains(mixed $value, bool $strict = true): bool;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $isEven = fn(int $n) => $n % 2 === 0;
*
* self::assertSame(Option\none()->filter($isEven), Option\none());
Expand All @@ -352,8 +322,6 @@ public function filter(callable $predicate): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $maybeSomeString = Option\some("Hello, World!");
* $maybeSomeLen = $maybeSomeString->map(strlen(...));
* self::assertEq($maybeSomeLen, Option\some(13));
Expand All @@ -372,8 +340,6 @@ public function map(callable $callback): Option;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some("foo");
* self::assertSame($x->mapOr(strlen(...), 42), 3);
* // @var Option<string> $x
Expand All @@ -395,8 +361,6 @@ public function mapOr(callable $callback, mixed $default): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $k = 21;
* $x = Option\some("foo");
* self::assertSame($x->mapOrElse(strlen(...), fn () => 2 * $k), 3);
Expand All @@ -421,8 +385,6 @@ public function mapOrElse(callable $callback, callable $default): mixed;
* # Examples
*
* ```
* use TH\Maybe\Option;
*
* $x = Option\some(1);
* $y = Option\some("hi");
* // @var Option<int> $z
Expand All @@ -443,7 +405,7 @@ public function zip(Option $option): Option;
* # Examples
*
* ```
* use TH\Maybe\{Option, Result};
* use TH\Maybe\Result;
*
* self::assertEq(Option\some("foo")->okOr(0), Result\ok("foo"));
* self::assertEq(Option\none()->okOr(0), Result\err(0));
Expand All @@ -462,7 +424,7 @@ public function okOr(mixed $err): Result;
* # Examples
*
* ```
* use TH\Maybe\{Option, Result};
* use TH\Maybe\Result;
*
* self::assertEq(Option\some("foo")->okOrElse(fn () => 0), Result\ok("foo"));
* self::assertEq(Option\none()->okOrElse(fn () => 0), Result\err(0));
Expand Down
Loading

0 comments on commit b27796a

Please sign in to comment.