Skip to content

Commit 5b79110

Browse files
authored
Merge pull request #85 from widmogrod/feature/make-list-more-lazy
Small refactoring
2 parents ed2167f + f8f37d9 commit 5b79110

File tree

11 files changed

+55
-19
lines changed

11 files changed

+55
-19
lines changed

src/Functional/functions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function filter(callable $predicate, Foldable $list = null)
288288
return curryN(2, function (callable $predicate, Foldable $list) {
289289
return reduce(function (Listt $list, $x) use ($predicate) {
290290
return $predicate($x)
291-
? ListtCons::of(function () use ($list, $x) {
291+
? new ListtCons(function () use ($list, $x) {
292292
return [$x, $list];
293293
}) : $list;
294294
}, fromNil(), $list);

src/Functional/infinit.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
function iterate(callable $fn, $a = null)
3030
{
3131
return curryN(2, function (callable $fn, $a): Listt {
32-
return ListtCons::of(function () use ($fn, $a) {
32+
return new ListtCons(function () use ($fn, $a) {
3333
return [$a, iterate($fn, $fn($a))];
3434
});
3535
})(...func_get_args());
@@ -50,7 +50,7 @@ function iterate(callable $fn, $a = null)
5050
*/
5151
function repeat($a)
5252
{
53-
return ListtCons::of(function () use ($a, &$list) {
53+
return new ListtCons(function () use ($a, &$list) {
5454
return [$a, repeat($a)];
5555
});
5656
}
@@ -102,7 +102,7 @@ function cycle(Listt $xs): Listt
102102
return cycle($xs);
103103
}
104104

105-
return ListtCons::of(function () use ($xs, $cycled, $cycle) {
105+
return new ListtCons(function () use ($xs, $cycled, $cycle) {
106106
return [head($cycled), $cycle($xs, tail($cycled))];
107107
});
108108
};

src/Functional/listt.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function fromSnapshotIterator(SnapshotIterator $i): Listt
5151
return fromNil();
5252
}
5353

54-
return ListtCons::of(function () use ($i) {
54+
return new ListtCons(function () use ($i) {
5555
return [
5656
$i->current(),
5757
fromSnapshotIterator($i->snapshot())
@@ -74,9 +74,7 @@ function fromSnapshotIterator(SnapshotIterator $i): Listt
7474
*/
7575
function fromValue($value): Listt
7676
{
77-
return ListtCons::of(function () use ($value) {
78-
return [$value, fromNil()];
79-
});
77+
return ListtCons::of($value);
8078
}
8179

8280
/**
@@ -115,9 +113,9 @@ function fromNil(): Listt
115113
*
116114
* @return Listt
117115
*/
118-
function concat(Foldable $xs)
116+
function concat(Foldable $xs): Listt
119117
{
120-
return foldr(function ($x, Listt $y) {
118+
return foldr(function (Foldable $x, Listt $y) {
121119
return foldr(prepend, $y, $x);
122120
}, fromNil(), $xs);
123121
}
@@ -137,7 +135,7 @@ function concat(Foldable $xs)
137135
function prepend($x, Listt $xs = null)
138136
{
139137
return curryN(2, function ($x, Listt $xs): Listt {
140-
return ListtCons::of(function () use ($x, $xs) {
138+
return new ListtCons(function () use ($x, $xs) {
141139
return [$x, $xs];
142140
});
143141
})(...func_get_args());

src/Functional/sublist.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function take(int $n, Listt $xs = null)
2828
return fromNil();
2929
}
3030

31-
return $xs::of(function () use ($n, $xs) {
31+
return new $xs(function () use ($n, $xs) {
3232
return [head($xs), take($n - 1, tail($xs))];
3333
});
3434
})(...func_get_args());

src/Functional/zipping.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function zip(Listt $xs, Listt $ys = null)
3030
$x = head($xs);
3131
$y = head($ys);
3232

33-
return ListtCons::of(function () use ($x, $y, $xs, $ys) {
33+
return new ListtCons(function () use ($x, $y, $xs, $ys) {
3434
return [
3535
[$x, $y],
3636
zip(tail($xs), tail($ys))
@@ -61,10 +61,10 @@ function unzip(Listt $xs): array
6161
[$x, $y] = head($xs);
6262

6363
return [
64-
ListtCons::of(function () use ($x, $xs) {
64+
new ListtCons(function () use ($x, $xs) {
6565
return [$x, unzip(tail($xs))[0]];
6666
}),
67-
ListtCons::of(function () use ($y, $xs) {
67+
new ListtCons(function () use ($y, $xs) {
6868
return [$y, unzip(tail($xs))[1]];
6969
}),
7070
];

src/Monad/Maybe/Just.php

+11
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ public function extract()
8181
{
8282
return $this->value;
8383
}
84+
85+
/**
86+
* foldl _ z Nothing = z
87+
* foldl f z (Just x) = f z x
88+
*
89+
* @inheritdoc
90+
*/
91+
public function reduce(callable $function, $accumulator)
92+
{
93+
return $function($accumulator, $this->value);
94+
}
8495
}

src/Monad/Maybe/Maybe.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
interface Maybe extends
1111
FantasyLand\Monad,
12+
FantasyLand\Foldable,
1213
Common\ValueOfInterface,
1314
FantasyLand\Monoid
1415
{

src/Monad/Maybe/Nothing.php

+11
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,15 @@ public function extract()
7373
{
7474
return null;
7575
}
76+
77+
/**
78+
* foldl _ z Nothing = z
79+
* foldl f z (Just x) = f z x
80+
*
81+
* @inheritdoc
82+
*/
83+
public function reduce(callable $function, $accumulator)
84+
{
85+
return $accumulator;
86+
}
7687
}

src/Primitive/ListtCons.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ class ListtCons implements Listt, \IteratorAggregate
1212
{
1313
public const of = 'Widmogrod\Primitive\ListtCons::of';
1414

15+
/**
16+
* @var callable
17+
*/
1518
private $next;
1619

1720
/**
1821
* @inheritdoc
1922
*/
2023
public static function of($value)
2124
{
22-
return new static($value);
25+
return new self(function () use ($value): array {
26+
return [$value, self::mempty()];
27+
});
2328
}
2429

2530
public function __construct(callable $next)
@@ -44,7 +49,7 @@ public function getIterator()
4449
*/
4550
public function map(callable $transformation): FantasyLand\Functor
4651
{
47-
return self::of(function () use ($transformation) {
52+
return new self(function () use ($transformation) {
4853
[$head, $tail] = $this->headTail();
4954

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

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

148153
return [$x, $xs->concat($value)];

src/Primitive/ListtNil.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ListtNil implements Listt
1111
{
1212
use Common\PointedTrait;
1313

14-
public const of = 'Widmogrod\Primitive\Listt::of';
14+
public const of = 'Widmogrod\Primitive\ListtConst::of';
1515

1616
public function __construct()
1717
{

test/Functional/ConcatTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace test\Functional;
66

77
use Widmogrod\Functional as f;
8+
use Widmogrod\Monad\Maybe\Just;
9+
use Widmogrod\Monad\Maybe\Nothing;
810

911
class ConcatTest extends \PHPUnit\Framework\TestCase
1012
{
@@ -59,6 +61,14 @@ public function provideData()
5961
3
6062
]),
6163
],
64+
'Just of lists' => [
65+
'$array' => Just::of(f\fromIterable(['a', 1, 3])),
66+
'$expected' => f\fromIterable(['a', 1, 3]),
67+
],
68+
'Nothing of lists' => [
69+
'$array' => Nothing::mempty(),
70+
'$expected' => f\fromNil()
71+
],
6272
];
6373
}
6474
}

0 commit comments

Comments
 (0)