-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociativeArray.php
More file actions
58 lines (44 loc) · 1.75 KB
/
Copy pathAssociativeArray.php
File metadata and controls
58 lines (44 loc) · 1.75 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace TMciver\Functional;
use TMciver\Functional\ObjectTypeclass\ObjectTraversable;
// Just a wrapper around the native PHP array to allow for cool FP goodness.
class AssociativeArray {
use ObjectTraversable;
protected $array;
public function __construct($array) {
$this->array = is_null($array) ? [] : $array;
}
// TODO: This is broken. It returns an instance of the given Monad wrapping
// a PHP _native_ array; not an AssociativeArray.
public function traverse(callable $f, $monad) {
// Initial value for the fold: an empty array wrapped in a default
// context.
$init = $monad->pure([]);
// Define the folding function.
$foldingFn = function ($acc, $curr) use ($f, $monad) {
// Call $f on the current value of the array, $curr. The return
// value should be a monadic value.
try {
$returnedMonad = $f($curr);
// If the result is null, we fail.
if (is_null($returnedMonad)) {
$returnedMonad = $monad->fail('The callable passed to `AssociativeArray::traverse` returned null.');
}
} catch (\Exception $e) {
$returnedMonad = $monad->fail('The callable passed to `AssociativeArray::traverse` threw an exception: ' . $e->getMessage());
}
// Put the value wrapped by the above monadic value in the array
// held by the accumulator, $acc, to get the new accumulator.
$newAcc = $returnedMonad->flatMap(function ($newVal) use ($acc) {
return $acc->map(function ($arr) use ($newVal) {
$arr[] = $newVal;
return $arr;
});
});
return $newAcc;
};
// Do the fold.
$result = array_reduce($this->array, $foldingFn, $init);
return $result;
}
}