-
Notifications
You must be signed in to change notification settings - Fork 8
/
IO.php
162 lines (146 loc) · 3.39 KB
/
IO.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
<?php
declare(strict_types=1);
namespace Marcosh\LamPHPda;
use Marcosh\LamPHPda\Brand\IOBrand;
use Marcosh\LamPHPda\HK\HK1;
use Marcosh\LamPHPda\Instances\IO\IOApplicative;
use Marcosh\LamPHPda\Instances\IO\IOApply;
use Marcosh\LamPHPda\Instances\IO\IOFunctor;
use Marcosh\LamPHPda\Instances\IO\IOMonad;
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/IO.md
*
* @template-covariant A
*
* @implements DefaultMonad<IOBrand, A>
*
* @psalm-immutable
*/
final class IO implements DefaultMonad
{
/** @var callable(): A */
private $action;
/**
* @param callable(): A $action
*/
private function __construct(callable $action)
{
$this->action = $action;
}
/**
* @template B
* @param callable(): B $action
* @return IO<B>
*
* @psalm-pure
*/
public static function action(callable $action): self
{
return new self($action);
}
/**
* @template B
* @param HK1<IOBrand, B> $hk
* @return IO<B>
*
* @psalm-pure
*/
public static function fromBrand(HK1 $hk): self
{
/** @var IO<B> */
return $hk;
}
/**
* @return A
*/
public function eval(): mixed
{
/** @psalm-suppress ImpureFunctionCall */
return ($this->action)();
}
/**
* @template B
* @param Functor<IOBrand> $functor
* @param callable(A): B $f
* @return IO<B>
*/
public function imap(Functor $functor, callable $f): self
{
return self::fromBrand($functor->map($f, $this));
}
/**
* @template B
* @param callable(A): B $f
* @return IO<B>
*/
public function map(callable $f): self
{
return $this->imap(new IOFunctor(), $f);
}
/**
* @template B
* @param Apply<IOBrand> $apply
* @param HK1<IOBrand, callable(A): B> $f
* @return IO<B>
*/
public function iapply(Apply $apply, HK1 $f): self
{
return self::fromBrand($apply->apply($f, $this));
}
/**
* @template B
* @param HK1<IOBrand, callable(A): B> $f
* @return IO<B>
*/
public function apply(HK1 $f): self
{
return $this->iapply(new IOApply(), $f);
}
/**
* @template B
* @param Applicative<IOBrand> $applicative
* @param B $a
* @return IO<B>
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
/**
* @template B
* @param B $a
* @return IO<B>
*
* @psalm-pure
*/
public static function pure(mixed $a): self
{
return self::ipure(new IOApplicative(), $a);
}
/**
* @template B
* @param Monad<IOBrand> $monad
* @param callable(A): HK1<IOBrand, B> $f
* @return IO<B>
*/
public function ibind(Monad $monad, callable $f): self
{
return self::fromBrand($monad->bind($this, $f));
}
/**
* @template B
* @param callable(A): HK1<IOBrand, B> $f
* @return IO<B>
*/
public function bind(callable $f): self
{
return $this->ibind(new IOMonad(), $f);
}
}