-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmiscellaneous.php
More file actions
150 lines (131 loc) · 2.43 KB
/
miscellaneous.php
File metadata and controls
150 lines (131 loc) · 2.43 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
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
<?php
declare(strict_types=1);
namespace Widmogrod\Functional;
/**
* @var callable
*/
const noop = 'Widmogrod\Functional\noop';
/**
* noop :: _ -> _
*
* @return null
*/
function noop()
{
return null;
}
/**
* @var callable
*/
const identity = 'Widmogrod\Functional\identity';
/**
* id :: a -> a
*
* Return value passed to function
*
* @param mixed $x
*
* @return mixed
*/
function identity($x)
{
return $x;
}
/**
* @var callable
*/
const constt = 'Widmogrod\Functional\constt';
/**
* const :: a -> b -> a
*
* const x is a unary function which evaluates to x for all inputs.
*
* For instance,
* ```haskell
* >>> map (const 42) [0..3]
* [42,42,42,42]
* ```
*
* @param $a
* @param $b
* @return callable
*/
function constt($a, $b = null)
{
return curryN(2, function ($a) {
return $a;
})(...func_get_args());
}
/**
* @var callable
*/
const compose = 'Widmogrod\Functional\compose';
/**
* (.) :: (b -> c) -> (a -> b) -> a -> c
*
* Compose multiple functions into one.
* Composition starts from right to left.
*
* <code>
* compose('strtolower', 'strtoupper')('aBc') ≡ 'abc'
* strtolower(strtouppser('aBc')) ≡ 'abc'
* </code>
*
* @param callable $a
* @param callable $b,...
*
* @return \Closure func($value) : mixed
*/
function compose(callable $a, callable $b)
{
return reverse(pipeline)(...func_get_args());
}
/**
* @var callable
*/
const pipeline = 'Widmogrod\Functional\pipeline';
/**
* Compose multiple functions into one.
* Composition starts from left.
*
* <code>
* compose('strtolower', 'strtoupper')('aBc') ≡ 'ABC'
* strtouppser(strtolower('aBc')) ≡ 'ABC'
* </code>
*
* @param callable $a
* @param callable $b,...
*
* @return \Closure func($value) : mixed
*/
function pipeline(callable $a, callable $b)
{
$list = func_get_args();
return function ($value = null) use (&$list) {
return array_reduce($list, function ($accumulator, callable $a) {
return $a($accumulator);
}, $value);
};
}
/**
* @var callable
*/
const flip = 'Widmogrod\Functional\flip';
/**
* flip :: (a -> b -> c) -> b -> a -> c
*
* @param callable $func
*
* @return callable
*/
function flip(callable $func)
{
$args = func_get_args();
array_shift($args);
return curryN(2, function ($a, $b) use ($func) {
$args = func_get_args();
$args[0] = $b;
$args[1] = $a;
return $func(...$args);
})(...$args);
}