Skip to content

Commit

Permalink
Merge pull request #85 from widmogrod/feature/make-list-more-lazy
Browse files Browse the repository at this point in the history
Small refactoring
  • Loading branch information
widmogrod authored Dec 30, 2017
2 parents ed2167f + f8f37d9 commit 5b79110
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Functional/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function filter(callable $predicate, Foldable $list = null)
return curryN(2, function (callable $predicate, Foldable $list) {
return reduce(function (Listt $list, $x) use ($predicate) {
return $predicate($x)
? ListtCons::of(function () use ($list, $x) {
? new ListtCons(function () use ($list, $x) {
return [$x, $list];
}) : $list;
}, fromNil(), $list);
Expand Down
6 changes: 3 additions & 3 deletions src/Functional/infinit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
function iterate(callable $fn, $a = null)
{
return curryN(2, function (callable $fn, $a): Listt {
return ListtCons::of(function () use ($fn, $a) {
return new ListtCons(function () use ($fn, $a) {
return [$a, iterate($fn, $fn($a))];
});
})(...func_get_args());
Expand All @@ -50,7 +50,7 @@ function iterate(callable $fn, $a = null)
*/
function repeat($a)
{
return ListtCons::of(function () use ($a, &$list) {
return new ListtCons(function () use ($a, &$list) {
return [$a, repeat($a)];
});
}
Expand Down Expand Up @@ -102,7 +102,7 @@ function cycle(Listt $xs): Listt
return cycle($xs);
}

return ListtCons::of(function () use ($xs, $cycled, $cycle) {
return new ListtCons(function () use ($xs, $cycled, $cycle) {
return [head($cycled), $cycle($xs, tail($cycled))];
});
};
Expand Down
12 changes: 5 additions & 7 deletions src/Functional/listt.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function fromSnapshotIterator(SnapshotIterator $i): Listt
return fromNil();
}

return ListtCons::of(function () use ($i) {
return new ListtCons(function () use ($i) {
return [
$i->current(),
fromSnapshotIterator($i->snapshot())
Expand All @@ -74,9 +74,7 @@ function fromSnapshotIterator(SnapshotIterator $i): Listt
*/
function fromValue($value): Listt
{
return ListtCons::of(function () use ($value) {
return [$value, fromNil()];
});
return ListtCons::of($value);
}

/**
Expand Down Expand Up @@ -115,9 +113,9 @@ function fromNil(): Listt
*
* @return Listt
*/
function concat(Foldable $xs)
function concat(Foldable $xs): Listt
{
return foldr(function ($x, Listt $y) {
return foldr(function (Foldable $x, Listt $y) {
return foldr(prepend, $y, $x);
}, fromNil(), $xs);
}
Expand All @@ -137,7 +135,7 @@ function concat(Foldable $xs)
function prepend($x, Listt $xs = null)
{
return curryN(2, function ($x, Listt $xs): Listt {
return ListtCons::of(function () use ($x, $xs) {
return new ListtCons(function () use ($x, $xs) {
return [$x, $xs];
});
})(...func_get_args());
Expand Down
2 changes: 1 addition & 1 deletion src/Functional/sublist.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function take(int $n, Listt $xs = null)
return fromNil();
}

return $xs::of(function () use ($n, $xs) {
return new $xs(function () use ($n, $xs) {
return [head($xs), take($n - 1, tail($xs))];
});
})(...func_get_args());
Expand Down
6 changes: 3 additions & 3 deletions src/Functional/zipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function zip(Listt $xs, Listt $ys = null)
$x = head($xs);
$y = head($ys);

return ListtCons::of(function () use ($x, $y, $xs, $ys) {
return new ListtCons(function () use ($x, $y, $xs, $ys) {
return [
[$x, $y],
zip(tail($xs), tail($ys))
Expand Down Expand Up @@ -61,10 +61,10 @@ function unzip(Listt $xs): array
[$x, $y] = head($xs);

return [
ListtCons::of(function () use ($x, $xs) {
new ListtCons(function () use ($x, $xs) {
return [$x, unzip(tail($xs))[0]];
}),
ListtCons::of(function () use ($y, $xs) {
new ListtCons(function () use ($y, $xs) {
return [$y, unzip(tail($xs))[1]];
}),
];
Expand Down
11 changes: 11 additions & 0 deletions src/Monad/Maybe/Just.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ public function extract()
{
return $this->value;
}

/**
* foldl _ z Nothing = z
* foldl f z (Just x) = f z x
*
* @inheritdoc
*/
public function reduce(callable $function, $accumulator)
{
return $function($accumulator, $this->value);
}
}
1 change: 1 addition & 0 deletions src/Monad/Maybe/Maybe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

interface Maybe extends
FantasyLand\Monad,
FantasyLand\Foldable,
Common\ValueOfInterface,
FantasyLand\Monoid
{
Expand Down
11 changes: 11 additions & 0 deletions src/Monad/Maybe/Nothing.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ public function extract()
{
return null;
}

/**
* foldl _ z Nothing = z
* foldl f z (Just x) = f z x
*
* @inheritdoc
*/
public function reduce(callable $function, $accumulator)
{
return $accumulator;
}
}
11 changes: 8 additions & 3 deletions src/Primitive/ListtCons.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ class ListtCons implements Listt, \IteratorAggregate
{
public const of = 'Widmogrod\Primitive\ListtCons::of';

/**
* @var callable
*/
private $next;

/**
* @inheritdoc
*/
public static function of($value)
{
return new static($value);
return new self(function () use ($value): array {
return [$value, self::mempty()];
});
}

public function __construct(callable $next)
Expand All @@ -44,7 +49,7 @@ public function getIterator()
*/
public function map(callable $transformation): FantasyLand\Functor
{
return self::of(function () use ($transformation) {
return new self(function () use ($transformation) {
[$head, $tail] = $this->headTail();

return [$transformation($head), $tail->map($transformation)];
Expand Down Expand Up @@ -142,7 +147,7 @@ public function concat(FantasyLand\Semigroup $value): FantasyLand\Semigroup
}

if ($value instanceof self) {
return self::of(function () use ($value) {
return new self(function () use ($value) {
[$x, $xs] = $this->headTail();

return [$x, $xs->concat($value)];
Expand Down
2 changes: 1 addition & 1 deletion src/Primitive/ListtNil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ListtNil implements Listt
{
use Common\PointedTrait;

public const of = 'Widmogrod\Primitive\Listt::of';
public const of = 'Widmogrod\Primitive\ListtConst::of';

public function __construct()
{
Expand Down
10 changes: 10 additions & 0 deletions test/Functional/ConcatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace test\Functional;

use Widmogrod\Functional as f;
use Widmogrod\Monad\Maybe\Just;
use Widmogrod\Monad\Maybe\Nothing;

class ConcatTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -59,6 +61,14 @@ public function provideData()
3
]),
],
'Just of lists' => [
'$array' => Just::of(f\fromIterable(['a', 1, 3])),
'$expected' => f\fromIterable(['a', 1, 3]),
],
'Nothing of lists' => [
'$array' => Nothing::mempty(),
'$expected' => f\fromNil()
],
];
}
}

0 comments on commit 5b79110

Please sign in to comment.