-
Notifications
You must be signed in to change notification settings - Fork 4
/
optionalib.php
427 lines (385 loc) · 7.88 KB
/
optionalib.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
* Monadic interface allows filtering, transforms, or safe
* mutation for object wrappers
*/
interface Monadic {
/**
* Filter
*
* @param callable $callback (mixed => boolean)
* @return mixed
*/
public function filter($callback);
/**
* Transform
*
* @param callable $callback (mixed => mixed)
* @return mixed
*/
public function map($callback);
/**
* Transforms and flattens
*
* @param callable $callback (mixed => mixed)
* @return mixed
*/
public function flatMap($callback);
/**
* Mutate
*
* @param callable $callback (mixed => void)
* @return mixed
*/
public function each($callback);
}
/**
* An option type is either carrying something or nothing
*/
abstract class Model_Option implements Monadic {
/**
* Is this option empty?
*
* @return boolean
*/
public abstract function isEmpty();
/**
* Get the wrapped object
*
* @return mixed
*/
public abstract function get();
/**
* Is this option defined?
*
* @return boolean
*/
public function isDefined() {
return !$this->isEmpty();
}
/**
* @see parent
* @return Model_Option
*/
public function filter($callback) {
if ($this->isDefined() && $callback($this->get())) {
return new Model_Some($this->get());
}
return new Model_None();
}
/**
* @see parent
* @return Model_Option
*/
public function map($callback) {
if (!$this->isEmpty()) {
return new Model_Some($callback($this->get()));
}
return $this;
}
/**
* @see parent
* @return Model_Option
*/
public function flatMap($callback) {
return $this->map(function($contained) use ($callback) {
if ($contained instanceof Monadic) {
return $contained->flatMap($callback);
} else {
return $callback($contained);
}
});
}
/**
* @see parent
* @return Model_Option
*/
public function each($callback) {
if (!$this->isEmpty()) {
$callback($this->get());
}
return $this;
}
/**
* Make this option a Some with callback return
*
* @param callable $callback (void => mixed)
* @return Model_Some
*/
public function orElse($callback) {
return $this->isDefined() ? $this : new Model_Some($callback());
}
/**
* Get the underlying value or some default value
*
* @param mixed $default
* @return mixed
*/
public function getOrElse($default) {
return $this->isDefined() ? $this->get() : $default;
}
}
/**
* An option type that is carrying something
*/
class Model_Some extends Model_Option {
protected $identity;
/**
* Fill this something with .... something
*
* @param mixed $identity
*/
public function __construct($identity) {
$this->identity = $identity;
}
/**
* @see parent
* @return boolean
*/
public function isEmpty() {
return false;
}
/**
* @see parent
* @return mixed
*/
public function get() {
return $this->identity;
}
}
/**
* An option type that is carrying nothing
*/
class Model_None extends Model_Option {
/**
* @see parent
* @return boolean
*/
public function isEmpty() {
return true;
}
/**
* @see parent
* @throws BadMethodCallException
*/
public function get() {
throw new BadMethodCallException("Can not call Model_None::get().");
}
}
/**
* A optional carrier that denotes one thing or another (maybe)
*/
abstract class Model_Either {
protected $identity;
/**
* The wrapped object
*
* @param $identity
*/
public function __construct($identity) {
$this->identity = $identity;
}
/**
* Is this maybe right leaning?
*
* @return boolean
*/
public abstract function isRight();
/**
* Is this maybe left leaning?
*
* @return boolean
*/
public function isLeft() {
return !$this->isRight();
}
/**
* Gets the wrapped object
*
* @return mixed
*/
public function get() {
return $this->identity;
}
/**
* Flattens this maybe into something from its left or right leaning
* positions
*
* @param callable $onfailure (left)
* @param callable $onsuccess (right)
* @return mixed
*/
public function fold($onfailure, $onsuccess) {
return $this->isRight() ?
$onsuccess($this->get()) :
$onfailure($this->get());
}
/**
* Create a projection that is only operable on right leaning maybe's
*
* @return Right_Projection
*/
public function withRight() {
return new Right_Projection($this);
}
/**
* Create a projection that is only operable on left leaning maybe's
*
* @return Left_Projection
*/
public function withLeft() {
return new Left_Projection($this);
}
}
/**
* A projection is a proxy object for maybes that allows
* Monadic operations on the underying Model_Either.
*
* Other than the Monadic calls, all other calls are forwarded to
* the underlying object.
*/
abstract class Either_Projection implements Monadic {
protected $identity;
/**
* Does this projection accept operations of this maybe?
*
* @return boolean
*/
public abstract function isAcceptable();
/**
* Re-wraps the projection with its appropriate maybe
*
* @param mixed $result
* @return Either_Projection
*/
public abstract function wrap($result);
/**
* Constructs this projection off of a maybe type
*
* @param Model_Either $either
*/
public function __construct(Model_Either $either) {
$this->identity = $either;
}
/**
* Forwards calls to the underlying maybe type
*
* @param string $name
* @param array $args
*/
public function __call($name, $args = array()) {
return call_user_func_array(array($this->identity, $name), $args);
}
/**
* @see parent
* @return Either_Projection
*/
public function map($callback) {
if ($this->isAcceptable()) {
return $this->wrap($callback($this->get()));
} else {
return $this;
}
}
/**
* @see parent
* @return Either_Projection
*/
public function flatMap($callback) {
return $this->map(function($contained) use ($callback) {
if ($contained instanceof Monadic) {
return $contained->flatMap($callback);
} else {
return $callback($contained);
}
});
}
/**
* @see parent
* @return Either_Projection
*/
public function each($callback) {
if ($this->isAcceptable()) {
$callback($this->get());
}
return $this;
}
/**
* @see parent
* @return Either_Projection
*/
public function filter($callback) {
if ($this->isAcceptable() && $callback($this->get())) {
return new Model_Some($this->wrap($this->get()));
}
return new Model_None();
}
}
/**
* A maybe type that is left leaning
*/
class Model_Left extends Model_Either {
/**
* @see parent
* @return boolean
*/
public function isRight() {
return false;
}
}
/**
* A maybe type that is right leaning
*/
class Model_Right extends Model_Either {
/**
* @see parent
* @return boolean
*/
public function isRight() {
return true;
}
}
/**
* A projection that is left leaning
*/
class Left_Projection extends Either_Projection {
/**
* Only accepts operations on a Model_Left
*
* @return boolean
*/
public function isAcceptable() {
return $this->isLeft();
}
/**
* @see parent
*
* @param mixed $result
* @return Left_Projection
*/
public function wrap($result) {
return new Left_Projection(new Model_Left($result));
}
}
/**
* A projection that is right leaning
*/
class Right_Projection extends Either_Projection {
/**
* Only accepts operations on a Model_Right
*
* @return boolean
*/
public function isAcceptable() {
return $this->isRight();
}
/**
* @see parent
*
* @param mixed $result
* @return Right_Projection
*/
public function wrap($result) {
return new Right_Projection(new Model_Right($result));
}
}