-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseApplicative.php
More file actions
40 lines (32 loc) · 1.01 KB
/
Copy pathBaseApplicative.php
File metadata and controls
40 lines (32 loc) · 1.01 KB
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
<?php
namespace TMciver\Functional\Typeclass;
/**
* An abstract class implementing the Applicative interface containing an
* implementation of the `__invoke()` magic method for convenience. Any custom
* Applicative instance you may write should extend this class.
*/
abstract class BaseApplicative implements Applicative {
function __invoke() {
// $ff, ...$fas
$args = func_get_args();
// make sure we were given at least one argument.
if (count($args) < 1) {
throw new \Exception('Must call Applicative::__invoke() with at least one argument.');
}
// Get Applicative function.
$ff = $args[0];
// Get the Applicative arguments.
array_shift($args);
$fas = $args;
$numArgs = count($fas);
if ($numArgs > 0) {
$callback = function ($applicativeFun, $applicativeArg) {
return $applicativeFun->apply($applicativeArg);
};
$result = array_reduce($fas, $callback, $ff);
} else {
$result = $this->apply($ff);
}
return $result;
}
}