-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLensStateIsYourFather.scala
268 lines (196 loc) · 7.48 KB
/
LensStateIsYourFather.scala
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package org.hablapps.gist
import org.scalatest._
import scalaz.{ Reader, State }
import scalaz.Isomorphism.<=>
import monocle.{ Getter, Lens, Optional, Setter, Fold }
/* IMPORTANT: this gist belongs to the blog post "Lens, State Is Your Father".
* Please, visit the following link to get a complete description of the
* contents:
*
* https://blog.hablapps.com/2016/11/10/lens-state-is-your-father/
*/
class LensStateIsYourFather extends FlatSpec with Matchers {
type IOCoalgebra[IOAlg[_[_]], Step[_, _], S] = IOAlg[Step[S, ?]]
object OpticsAsCoalgebras {
import scalaz.{ Functor, Monad }
import scalaz.syntax.monad._
/* IOLens */
trait LensAlg[A, P[_]] {
def get: P[A]
def set(a: A): P[Unit]
def gets[B](
f: A => B)(implicit
F: Functor[P]): P[B] =
get map f
def modify(
f: A => A)(implicit
M: Monad[P]): P[Unit] =
get >>= (f andThen set)
}
type IOLens[S, A] = IOCoalgebra[LensAlg[A, ?[_]], State, S]
object IOLens {
def apply[S, A](_get: S => A)(_set: A => S => S): IOLens[S, A] =
new LensAlg[A, State[S, ?]] {
def get: State[S, A] = State.gets(_get)
def set(a: A): State[S, Unit] = State.modify(_set(a))
}
def lensIso[S, A] = new (Lens[S, A] <=> IOLens[S, A]) {
def from: IOLens[S, A] => Lens[S, A] =
ioln => Lens[S, A](ioln.get.eval)(a => ioln.set(a).exec)
def to: Lens[S, A] => IOLens[S, A] = ln => new IOLens[S, A] {
def get: State[S, A] = State.gets(ln.get)
def set(a: A): State[S, Unit] = State.modify(ln.set(a))
}
}
}
/* IOOptional */
trait OptionalAlg[A, P[_]] {
def getOption: P[Option[A]]
def set(a: A): P[Unit]
}
type IOOptional[S, A] = IOCoalgebra[OptionalAlg[A, ?[_]], State, S]
object IOOptional {
def optionalIso[S, A] = new (Optional[S, A] <=> IOOptional[S, A]) {
def from: IOOptional[S, A] => Optional[S, A] =
ioopt => Optional[S, A](ioopt.getOption.eval)(a => ioopt.set(a).exec)
def to: Optional[S, A] => IOOptional[S, A] = opt => new IOOptional[S, A] {
def getOption: State[S, Option[A]] = State.gets(opt.getOption)
def set(a: A): State[S, Unit] = State.modify(opt.set(a))
}
}
}
/* IOSetter */
trait SetterAlg[A, P[_]] {
def modify(f: A => A): P[Unit]
}
type IOSetter[S, A] = IOCoalgebra[SetterAlg[A, ?[_]], State, S]
object IOSetter {
def setterIso[S, A] = new (Setter[S, A] <=> IOSetter[S, A]) {
def from: IOSetter[S, A] => Setter[S, A] =
iost => Setter[S, A](f => iost.modify(f).exec)
def to: Setter[S, A] => IOSetter[S, A] = st => new IOSetter[S, A] {
def modify(f: A => A): State[S, Unit] = State.modify(st.modify(f))
}
}
}
/* IOGetter */
trait GetterAlg[A, P[_]] {
def get: P[A]
}
type IOGetter[S, A] = IOCoalgebra[GetterAlg[A, ?[_]], Reader, S]
object IOGetter {
def getterIso[S, A] = new (Getter[S, A] <=> IOGetter[S, A]) {
def from: IOGetter[S, A] => Getter[S, A] =
iogt => Getter[S, A](iogt.get.run)
def to: Getter[S, A] => IOGetter[S, A] = gt => new IOGetter[S, A] {
def get: Reader[S, A] = Reader(gt.get)
}
}
}
}
object OpticsAndStateConnections {
import scalaz.{ Monad, MonadState }
import OpticsAsCoalgebras.IOLens
type MSLens[S, A] = MonadState[State[S, ?], A]
def lensIso[S, A] = new (IOLens[S, A] <=> MSLens[S, A]) {
def from: MSLens[S, A] => IOLens[S, A] = msln => new IOLens[S, A] {
def get: State[S, A] = msln.get
def set(a: A): State[S, Unit] = msln.put(a)
}
def to: IOLens[S, A] => MSLens[S, A] = ioln => new MSLens[S, A] {
private val SM: Monad[State[S, ?]] = Monad[State[S, ?]]
def point[A](a: => A): State[S, A] = SM.point(a)
def bind[A, B](fa: State[S, A])(f: A => State[S, B]): State[S, B] =
SM.bind(fa)(f)
def get: State[S, A] = ioln.get
def put(a: A): State[S, Unit] = ioln.set(a)
def init: State[S, A] = get
}
}
}
object MonocleAndState {
import Function.const
import scalaz.syntax.monad._
import monocle.macros.GenLens
import monocle.state.all._
import OpticsAsCoalgebras.IOLens
case class Person(name: String, age: Int)
val p: Person = Person("John", 30)
/* Example using Monocle's state module */
val _age: Lens[Person, Int] = GenLens[Person](_.age)
val increment: State[Person, Int] = _age mod (_ + 1)
increment.run(p) shouldEqual (Person("John", 31), 31)
/* Example using IOLens (returns Unit instead) */
val _ioage: IOLens[Person, Int] =
IOLens[Person, Int](_.age)(age => _.copy(age = age))
val ioincrement: State[Person, Int] =
(_ioage modify (_ + 1)) >> (_ioage.get)
ioincrement.run(p) shouldEqual (Person("John", 31), 31)
}
"IOLens" should "work" in MonocleAndState
object DiscussionAndOngoingWork {
import scalaz.IndexedState
import monocle.PLens
type IOCoalgebra[IOAlg[_[_], _[_]], Step[_, _, _], S, T] =
IOAlg[Step[S, S, ?], Step[S, T, ?]]
trait PBind[F[_], G[_], H[_]] {
def pbind[A, B](fa: F[A])(f: A => G[B]): H[B]
}
object PBind {
private type IS[S, T, A] = IndexedState[S, T, A]
implicit def IndexedStateInstance[S1, S2, S3] =
new PBind[IS[S1, S2, ?], IS[S2, S3, ?], IS[S1, S3, ?]] {
def pbind[A, B](
fa: IS[S1, S2, A])(
f: A => IS[S2, S3, B]): IS[S1, S3, B] =
fa flatMap f
}
}
trait PLensAlg[A, B, P[_], Q[_]] {
def get: P[A]
def set(b: B): Q[Unit]
import scalaz.{ Functor, Monad }
import scalaz.Isomorphism.<=>
import scalaz.syntax.monad._
def gets[C](
f: A => C)(implicit
F: Functor[P]): P[C] =
get map f
def modify[H[_]](
f: A => B)(implicit
PB: PBind[P, Q, H]): H[Unit] =
PB.pbind(get)(f andThen set)
}
type IOPLens[S, T, A, B] =
IOCoalgebra[PLensAlg[A, B, ?[_], ?[_]], IndexedState, S, T]
object IOPLens {
def apply[S, T, A, B](
_get: S => A)(
_set: B => S => T): IOPLens[S, T, A, B] = new IOPLens[S, T, A, B] {
def get: IndexedState[S, S, A] =
IndexedState(s => (s, _get(s)))
def set(b: B): IndexedState[S, T, Unit] =
IndexedState(s => (_set(b)(s), ()))
}
def plensIso[S, T, A, B] = new (PLens[S, T, A, B] <=> IOPLens[S, T, A, B]) {
def from: IOPLens[S, T, A, B] => PLens[S, T, A, B] =
ioln => PLens(ioln.get.eval)(b => ioln.set(b).exec)
def to: PLens[S, T, A, B] => IOPLens[S, T, A, B] =
ln => new IOPLens[S, T, A, B] {
def get: IndexedState[S, S, A] =
IndexedState(s => (s, ln.get(s)))
def set(b: B): scalaz.IndexedState[S, T, Unit] =
IndexedState(s => (ln.set(b)(s), ()))
}
}
}
def _second[A, B, C]: IOPLens[(A, B), (A, C), B, C] =
IOPLens[(A, B), (A, C), B, C](_._2)(b => s => (s._1, b))
val tp: (Int, String) = (1, "hi")
_second[Int, String, Nothing].get.eval(tp) shouldEqual "hi"
_second[Int, String, Nothing].gets(_.length).eval(tp) shouldEqual 2
_second[Int, String, Int].modify(_.length).exec(tp) shouldEqual ((1, 2))
_second[Int, String, Char].set('a').exec(tp) shouldEqual ((1, 'a'))
}
"IOPLens" should "work" in DiscussionAndOngoingWork
}