-
Notifications
You must be signed in to change notification settings - Fork 8
/
Reader.php
182 lines (165 loc) · 3.97 KB
/
Reader.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
<?php
declare(strict_types=1);
namespace Marcosh\LamPHPda;
use Marcosh\LamPHPda\Brand\ReaderBrand;
use Marcosh\LamPHPda\HK\HK1;
use Marcosh\LamPHPda\Instances\Reader\ReaderApplicative;
use Marcosh\LamPHPda\Instances\Reader\ReaderApply;
use Marcosh\LamPHPda\Instances\Reader\ReaderFunctor;
use Marcosh\LamPHPda\Instances\Reader\ReaderMonad;
use Marcosh\LamPHPda\Typeclass\Applicative;
use Marcosh\LamPHPda\Typeclass\Apply;
use Marcosh\LamPHPda\Typeclass\DefaultInstance\DefaultMonad;
use Marcosh\LamPHPda\Typeclass\Functor;
use Marcosh\LamPHPda\Typeclass\Monad;
/**
* @see https://github.com/marcosh/lamphpda/tree/master/docs/data-structures/Reader.md
*
* @template E
* @template-covariant A
*
* @implements DefaultMonad<ReaderBrand<E>, A>
*
* @psalm-immutable
*/
final class Reader implements DefaultMonad
{
/** @var callable(E): A */
private $action;
/**
* @param callable(E): A $f
*/
private function __construct(callable $f)
{
$this->action = $f;
}
/**
* @template F
* @template B
* @param callable(F): B $f
* @return Reader<F, B>
*
* @psalm-pure
*/
public static function reader(callable $f): self
{
return new self($f);
}
/**
* @template B
* @template F
* @param HK1<ReaderBrand<F>, B> $b
* @return Reader<F, B>
*
* @psalm-pure
*/
public static function fromBrand(HK1 $b): self
{
/** @var Reader<F, B> */
return $b;
}
/**
* @param E $environment
* @return A
*/
public function runReader(mixed $environment): mixed
{
/** @psalm-suppress ImpureFunctionCall */
return ($this->action)($environment);
}
/**
* @return Reader<E, E>
*/
public function ask(): self
{
return self::reader(
/**
* @param E $e
* @return E
*/
static fn (mixed $e): mixed => $e
);
}
/**
* @template B
* @param Functor<ReaderBrand<E>> $functor
* @param callable(A): B $f
* @return Reader<E, B>
*/
public function imap(Functor $functor, callable $f): self
{
return self::fromBrand($functor->map($f, $this));
}
/**
* @template B
* @param callable(A): B $f
* @return Reader<E, B>
*/
public function map(callable $f): self
{
return $this->imap(new ReaderFunctor(), $f);
}
/**
* @template B
* @param Apply<ReaderBrand<E>> $apply
* @param HK1<ReaderBrand<E>, callable(A): B> $f
* @return Reader<E, B>
*/
public function iapply(Apply $apply, HK1 $f): self
{
return self::fromBrand($apply->apply($f, $this));
}
/**
* @template B
* @param HK1<ReaderBrand<E>, callable(A): B> $f
* @return Reader<E, B>
*/
public function apply(HK1 $f): self
{
return $this->iapply(new ReaderApply(), $f);
}
/**
* @template B
* @template F
* @param Applicative<ReaderBrand<F>> $applicative
* @param B $a
* @return Reader<F, B>
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
/**
* @template B
* @template F
* @param B $a
* @return Reader<F, B>
*
* @psalm-pure
*/
public static function pure(mixed $a): self
{
return self::ipure(new ReaderApplicative(), $a);
}
/**
* @template B
* @param Monad<ReaderBrand<E>> $monad
* @param callable(A): HK1<ReaderBrand<E>, B> $f
* @return Reader<E, B>
*/
public function ibind(Monad $monad, callable $f): self
{
return self::fromBrand($monad->bind($this, $f));
}
/**
* @template B
* @param callable(A): HK1<ReaderBrand<E>, B> $f
* @return Reader<E, B>
*/
public function bind(callable $f): self
{
return $this->ibind(new ReaderMonad(), $f);
}
}