-
Notifications
You must be signed in to change notification settings - Fork 8
/
Maybe.php
331 lines (303 loc) · 7.74 KB
/
Maybe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
declare(strict_types=1);
namespace Marcosh\LamPHPda;
use Marcosh\LamPHPda\Brand\Brand;
use Marcosh\LamPHPda\Brand\MaybeBrand;
use Marcosh\LamPHPda\HK\HK1;
use Marcosh\LamPHPda\Instances\Maybe\MaybeApplicative;
use Marcosh\LamPHPda\Instances\Maybe\MaybeApply;
use Marcosh\LamPHPda\Instances\Maybe\MaybeFoldable;
use Marcosh\LamPHPda\Instances\Maybe\MaybeFunctor;
use Marcosh\LamPHPda\Instances\Maybe\MaybeMonad;
use Marcosh\LamPHPda\Instances\Maybe\MaybeTraversable;
use Marcosh\LamPHPda\Typeclass\Applicative;
use Marcosh\LamPHPda\Typeclass\Apply;
use Marcosh\LamPHPda\Typeclass\DefaultInstance\DefaultMonad;
use Marcosh\LamPHPda\Typeclass\DefaultInstance\DefaultTraversable;
use Marcosh\LamPHPda\Typeclass\Foldable;
use Marcosh\LamPHPda\Typeclass\Functor;
use Marcosh\LamPHPda\Typeclass\Monad;
use Marcosh\LamPHPda\Typeclass\Traversable;
/**
* @see https://github.com/marcosh/lamphpda/tree/master/docs/data-structures/Maybe.md
*
* @template-covariant A
*
* @implements DefaultMonad<MaybeBrand, A>
* @implements DefaultTraversable<MaybeBrand, A>
*
* @psalm-immutable
*/
final class Maybe implements DefaultMonad, DefaultTraversable
{
/**
* @param null|A $value
*/
private function __construct(private readonly bool $isJust, private readonly mixed $value = null)
{
}
/**
* @template B
* @param B $value
* @return Maybe<B>
*
* @psalm-pure
*/
public static function just($value): self
{
return new self(true, $value);
}
/**
* @template B
* @return Maybe<B>
*
* @psalm-pure
*/
public static function nothing(): self
{
return new self(false);
}
/**
* @template B
* @param HK1<MaybeBrand, B> $b
* @return Maybe<B>
*
* @psalm-pure
*/
public static function fromBrand(HK1 $b): self
{
/** @var Maybe $b */
return $b;
}
/**
* this might not do what you expect when B contains null.
*
* @template B
* @param null|B $a
* @return Maybe<B>
*/
public static function fromNullable(mixed $a): self
{
if (null === $a) {
return self::nothing();
}
return self::just($a);
}
/**
* @return null|A
*/
public function toNullable()
{
return $this->eval(
null,
/**
* @param A $a
* @return null|A
*/
static fn (mixed $a): mixed => $a
);
}
/**
* @template B
* @param B $ifNothing
* @param callable(A): B $ifJust
* @return B
*/
public function eval(
$ifNothing,
callable $ifJust
): mixed {
if ($this->isJust) {
/**
* @psalm-suppress PossiblyNullArgument
* @psalm-suppress ImpureFunctionCall
*/
return $ifJust($this->value);
}
return $ifNothing;
}
/**
* @param A $a
* @return A
*/
public function withDefault(mixed $a): mixed
{
return $this->eval(
$a,
/**
* @param A $a
* @return A
*/
static fn (mixed $a): mixed => $a
);
}
/**
* @param callable(): A $a
* @return A
*/
public function withLazyDefault(callable $a): mixed
{
// This can be implemented in terms of eval(), however the actual implementation is optimized:
// return $this->eval($ifNothing, fn($value) => fn() => $ifJust($value))();
/**
* @psalm-suppress ImpureFunctionCall
*/
return $this->isJust
? $this->value
: $a();
}
/**
* @template B
* @param B $b
* @return Either<B, A>
*/
public function toEither(mixed $b): Either
{
return $this->eval(
Either::left($b),
static fn (mixed $a): Either => Either::right($a)
);
}
/**
* @template B
* @param Functor<MaybeBrand> $functor
* @param callable(A): B $f
* @return Maybe<B>
*/
public function imap(Functor $functor, callable $f): self
{
/** @psalm-suppress ArgumentTypeCoercion */
return self::fromBrand($functor->map($f, $this));
}
/**
* @template B
* @param callable(A): B $f
* @return Maybe<B>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public function map(callable $f): self
{
return $this->imap(new MaybeFunctor(), $f);
}
/**
* @template B
* @param Apply<MaybeBrand> $apply
* @param HK1<MaybeBrand, callable(A): B> $f
* @return Maybe<B>
*/
public function iapply(Apply $apply, HK1 $f): self
{
/** @psalm-suppress ArgumentTypeCoercion */
return self::fromBrand($apply->apply($f, $this));
}
/**
* @template B
* @param HK1<MaybeBrand, callable(A): B> $f
* @return Maybe<B>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public function apply(HK1 $f): self
{
return $this->iapply(new MaybeApply(), $f);
}
/**
* @template B
* @param Applicative<MaybeBrand> $applicative
* @param B $a
* @return Maybe<B>
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, $a): self
{
return self::fromBrand($applicative->pure($a));
}
/**
* @template B
* @param B $a
* @return Maybe<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public static function pure(mixed $a): self
{
return self::ipure(new MaybeApplicative(), $a);
}
/**
* @template B
* @param Monad<MaybeBrand> $monad
* @param callable(A): HK1<MaybeBrand, B> $f
* @return Maybe<B>
*/
public function ibind(Monad $monad, callable $f): self
{
/** @psalm-suppress ArgumentTypeCoercion */
return self::fromBrand($monad->bind($this, $f));
}
/**
* @template B
* @param callable(A): HK1<MaybeBrand, B> $f
* @return Maybe<B>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public function bind(callable $f): self
{
return $this->ibind(new MaybeMonad(), $f);
}
/**
* @template B
* @param Foldable<MaybeBrand> $foldable
* @param callable(A, B): B $f
* @param B $b
* @return B
*/
public function ifoldr(Foldable $foldable, callable $f, mixed $b)
{
/** @psalm-suppress ArgumentTypeCoercion */
return $foldable->foldr($f, $b, $this);
}
/**
* @template B
* @param callable(A, B): B $f
* @param B $b
* @return B
*/
public function foldr(callable $f, mixed $b): mixed
{
return $this->ifoldr(new MaybeFoldable(), $f, $b);
}
/**
* @template F of Brand
* @template B
* @param Traversable<MaybeBrand> $traversable
* @param Applicative<F> $applicative
* @param callable(A): HK1<F, B> $f
* @return HK1<F, Maybe<B>>
*/
public function itraverse(Traversable $traversable, Applicative $applicative, callable $f): HK1
{
/**
* @psalm-suppress InvalidArgument
* @psalm-suppress ArgumentTypeCoercion
*/
return $applicative->map([self::class, 'fromBrand'], $traversable->traverse($applicative, $f, $this));
}
/**
* @template F of Brand
* @template B
* @param Applicative<F> $applicative
* @param callable(A): HK1<F, B> $f
* @return HK1<F, Maybe<B>>
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public function traverse(Applicative $applicative, callable $f): HK1
{
return $this->itraverse(new MaybeTraversable(), $applicative, $f);
}
}