-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEither.php
192 lines (175 loc) · 3.64 KB
/
Either.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
<?php
declare(strict_types = 1);
namespace Innmind\Immutable;
use Innmind\Immutable\Either\{
Implementation,
Left,
Right,
Defer,
};
/**
* @template-covariant L
* @template-covariant R
* @psalm-immutable
*/
final class Either
{
/** @var Implementation<L, R> */
private Implementation $either;
/**
* @param Implementation<L, R> $either
*/
private function __construct(Implementation $either)
{
$this->either = $either;
}
/**
* @template A
* @template B
* @psalm-pure
*
* @param A $value
*
* @return self<A, B>
*/
public static function left($value): self
{
return new self(new Left($value));
}
/**
* @template A
* @template B
* @psalm-pure
*
* @param B $value
*
* @return self<A, B>
*/
public static function right($value): self
{
return new self(new Right($value));
}
/**
* This method is to be used for IO operations
*
* @template A
* @template B
* @psalm-pure
*
* @param callable(): self<A, B> $deferred
*
* @return self<A, B>
*/
public static function defer(callable $deferred): self
{
return new self(new Defer($deferred));
}
/**
* @template T
*
* @param callable(R): T $map
*
* @return self<L, T>
*/
public function map(callable $map): self
{
return new self($this->either->map($map));
}
/**
* @template A
* @template B
*
* @param callable(R): Either<A, B> $map
*
* @return Either<L|A, B>
*/
public function flatMap(callable $map): self
{
return $this->either->flatMap($map);
}
/**
* @template T
*
* @param callable(L): T $map
*
* @return self<T, R>
*/
public function leftMap(callable $map): self
{
return new self($this->either->leftMap($map));
}
/**
* @template T
*
* @param callable(R): T $right
* @param callable(L): T $left
*
* @return T
*/
public function match(callable $right, callable $left)
{
return $this->either->match($right, $left);
}
/**
* @template A
* @template B
*
* @param callable(L): Either<A, B> $otherwise
*
* @return Either<A, R|B>
*/
public function otherwise(callable $otherwise): self
{
return $this->either->otherwise($otherwise);
}
/**
* @template A
*
* @param callable(R): bool $predicate
* @param callable(): A $otherwise
*
* @return self<L|A, R>
*/
public function filter(callable $predicate, callable $otherwise): self
{
return new self($this->either->filter($predicate, $otherwise));
}
/**
* @return Maybe<R>
*/
public function maybe(): Maybe
{
return $this->either->maybe();
}
/**
* Force loading the value in memory (only useful for a deferred Either)
*
* @return self<L, R>
*/
public function memoize(): self
{
return $this->either->memoize();
}
/**
* Switch the sides of the values, left becomes right and right left
*
* @return self<R, L>
*/
public function flip(): self
{
return new self($this->either->flip());
}
/**
* @template A
* @template B
*
* @param callable(R): self<A, B> $right
* @param callable(L): self<A, B> $left
*
* @return self<A, B>
*/
public function eitherWay(callable $right, callable $left): self
{
return $this->either->eitherWay($right, $left);
}
}