-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectFoldable.php
More file actions
75 lines (67 loc) · 2.77 KB
/
Copy pathObjectFoldable.php
File metadata and controls
75 lines (67 loc) · 2.77 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace PhatCats\ObjectTypeclass;
use PhatCats\Collection;
use PhatCats\Typeclass\Monoid;
trait ObjectFoldable {
// not sure if it's a good idea to extend Collection . . .
use Collection;
/**
* Left-associative fold of a structure.
*
* @param $init The seed value for the fold. This value is the result in the
* case of an empty structure.
* @param $f The folding function. A function of two arguments: the first
* argument should be of the same type as $init (and the return type);
* the second argument is an element of the `ObjectFoldable` structure.
* @return A value that has the same type as $init.
*/
abstract function foldLeft($init, callable $f);
/**
* Right-associative fold of a structure.
*
* @param $init The seed value for the fold. This value is the result in the
* case of an empty structure.
* @param $f The folding function. A function of two arguments: the first
* argument is an element of the `ObjectFoldable` structure; the second
* should be of the same type as $init (and the return type).
* @return A value that has the same type as $init.
*/
abstract function foldRight($init, callable $f);
/**
* Folds the structure by mapping each element to a Monoid (using $toMonoid)
* and then combining the results using that Monoid.
*
* This method has a default implementation (which uses foldLeft) but can be
* overridden if a given type can provide a more efficient implementation.
*
* @param $monoid an instance of a monoid. Should be the same type as the
* return type of $toMonoid as well as the return type of foldMap.
* Needed for the case of an empty structure.
* @param $toMonoid A function that takes an element of the structure and
* returns a Monoid.
* @return A value that has the same type as the return type of $toMonoid and
* $monoid.
*/
public function foldMap(Monoid $monoid, callable $toMonoid) {
$init = $monoid->identity();
$f = function ($acc, $x) use ($monoid, $toMonoid) {
return $monoid->append($acc, $toMonoid($x));
};
return $this->foldLeft($init, $f);
}
/**
* Folds the structure by combining elements using a monoid. Note that the
* elements must already be a monoid.
*
* This method has a default implementation (which uses foldMap) but can be
* overridden if a given type can provide a more efficient implementation.
*
* @param $monoid an instance of a monoid. Should be the same type as the
* elements. Needed for the case of an empty structure.
* @return a value of the same type as the elements.
*/
public function fold(Monoid $monoid) {
$id = function ($x) { return $x; };
return $this->foldMap($monoid, $id);
}
}