Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
rearrange function arguments in fold
Browse files Browse the repository at this point in the history
this allows typechecking with the lambda passed to fold
also upgraded Result types to use templates
  • Loading branch information
ylixir committed Feb 18, 2019
1 parent 322671d commit 80e7463
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Similar to `array_reduce`, this function can be used to combine values. For exam
$flower = p::lit("flower");
$flowers = $flower->and($flower);

$parser = $flowers->fold(function (array $acc, string $in): array {
$parser = $flowers->fold(function (string $in, string ...$acc): array {
return ["flowers"];
}, []);

Expand All @@ -118,7 +118,7 @@ $flower = p::lit("flower");
$flowers = p::and($flower, $flower);

$parser = p::fold(
function (array $acc, string $in): array {
function (string $in, string ...$acc): array {
return ["flowers"];
},
[],
Expand Down
20 changes: 16 additions & 4 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public static function end(callable $p): callable
}

/**
* @param callable(array, mixed):array $f
* @template S
* @template T
* @param callable(T, S...):array<int,S> $f
* @param callable(string):?r $p
* @return callable(string):?r
*/
Expand All @@ -101,9 +103,18 @@ public static function fold(
if (null === $r) {
return $r;
} else {
/** @var array */ $reduced = array_reduce(
$ftransform =
/**
* @param S[] $a
* @param T $s
* @return array<int, S>
*/
function (array $a, $s) use ($f): array {
return $f($s, ...$a);
};
/** @var array<int, S> */ $reduced = array_reduce(
$r->parsed,
$f,
$ftransform,
$start
);
return r::make($r->unparsed, $reduced);
Expand Down Expand Up @@ -149,7 +160,8 @@ public static function map(callable $f, callable $p): callable
if (null === $r) {
return $r;
} else {
return r::make($r->unparsed, array_map($f, $r->parsed));
/** @var array<int, T> */ $mapped = array_map($f, $r->parsed);
return r::make($r->unparsed, $mapped);
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/Oop.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public function end(): self
}

/**
* @param callable(array, mixed):array $f
* @template T
* @template S
* @param callable(T, S...):S[] $f
* @param array<int, S> $start
*/
public function fold(callable $f, array $start = []): self
{
Expand Down
12 changes: 10 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@
namespace Phap;

/**
* @template T
* @property string $unparsed
* @property array<int,mixed> $parsed
* @property array<int,T> $parsed
* @psalm-seal-properties
*/
final class Result
{
/** @var string */ private $unparsed;
/** @var array */ private $parsed;
/** @var array<int, T> */ private $parsed;

public function __get(string $prop)
{
return $this->$prop;
}

/**
* @param array<int, T> $parsed
*/
private function __construct(string $unparsed, array $parsed)
{
$this->unparsed = $unparsed;
$this->parsed = $parsed;
}

/**
* @param array<int, T> $parsed
*/
public static function make(string $unparsed, array $parsed): self
{
return new self($unparsed, $parsed);
Expand Down
7 changes: 2 additions & 5 deletions test/Integration/OopExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public static function integerParser(): p
$intArray = $allDigits->map('intval');
//reduce the separate digits into one
$integer = $intArray->fold(
/**
* @param array{0:int} $a
*/
function (array $a, int $i): array {
return [$a[0] * 10 + $i];
function (int $i, int $a, int ...$tail): array {
return [$a * 10 + $i];
},
[0]
);
Expand Down
7 changes: 2 additions & 5 deletions test/Integration/functional_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ public static function integer_parser(): callable

//reduce the separate digits into one
$integer = p::fold(
/**
* @param array{0:int} $a
*/
function (array $a, int $i): array {
return [$a[0] * 10 + $i];
function (int $i, int $a, int ...$tail): array {
return [$a * 10 + $i];
},
[0],
$intArray
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_end(

public function fold_provider(): array
{
$fold = function (array $a, string $s): array {
$fold = function (string $s, ...$a): array {
if ('2' !== $s) {
$a[] = $s;
}
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/OopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testPop(string $input, ?r $expected): void

public function foldProvider(): array
{
$fold = function (array $a, string $s): array {
$fold = function (string $s, ...$a): array {
if ('2' !== $s) {
$a[] = $s;
}
Expand Down

0 comments on commit 80e7463

Please sign in to comment.