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
| package scalaz
import Scalaz._
import scala.collection.generic.{Subtractable, Addable}
import scala.collection.{SetLike, SeqLike}
import scala.collection.immutable._
import scala._
/**
* Lenses are required to satisfy the following two laws and to be side-effect free
*
* forall b. lens(lens.set(a,b)) = b
* forall a. lens.set(a,lens(a)) = a
*/
case class Lens[A,B](get: A => B, set: (A,B) => A) extends Function1[A,B] with Immutable {
def apply(whole:A): B = get(whole)
def mod(a:A, f: B => B) = set(a, f(this(a)))
def modf[F[_]:Functor](a:A, f: B => F[B]): F[A] = f(this(a)).map((b:B) => set(a,b))
def modp[C](a: A, f: B => (B, C)): (A, C) = {
val (b,c) = f(this(a))
(set(a,b),c)
}
def compose[C](that: Lens[C,A]) = Lens[C,B](
c => this(that(c)),
(c, b) => that.mod(c, set(_, b))
)
def andThen[C](that: Lens[B,C]) = that compose this
/** This requires that f and g witness an isomorphism between B and C */
def xmap[C](f: B => C)(g: C => B) = Lens[A,C](
a => f(this(a)),
(a,c) => set(a,g(c))
)
def |||[C](that: Lens[C,B]) = Lens[Either[A,C],B](
_.fold(this,that),
{
case (Left(a), b) => Left (set(a,b))
case (Right(c), b) => Right(that.set(c,b))
}
)
def ***[C,D](that: Lens[C,D]) = Lens[(A,C),(B,D)](
ac => (this(ac._1), that(ac._2)),
(ac,bd) => (set(ac._1,bd._1),that.set(ac._2,bd._2))
)
// these sadly fail lens laws
// def &&&[C] (that: Lens[A,C]): Lens[A,(B,C)]
// def +++[C,D](that: Lens[C,D]): Lens[Either[A,C],Either[B,D]]
// contravariantly map the state of a state monad through a lens
def toState : State[A,B] = state[A,B](a => (a, this(a)))
def lifts[C](s: State[B,C]) : State[A,C] = state[A,C](a => modp(a,s.apply))
def modps[C](f: B => (B,C)) : State[A,C] = lifts(state(f))
def mods[C](f : B => B) : State[A,B] = state[A,B](a => modp(a,f(_).pair))
def mods_[C](f : B => B) : State[A,Unit] = state[A,Unit](a => (mod(a,f), Unit))
def :=(b: B) : State[A,Unit] = state[A,Unit](a => (set(a,b), Unit))
def flatMap[C](f : B => State[A,C]) : State[A,C] = state[A,C](a => f(this(a))(a))
def map[C](f: B => C) : State[A,C] = state[A,C](a => (a,f(this(a))))
// override def toString() = "Lens[" + classOf[A].getShortName + "," + classOf[B].getShortName + "]"
}
object Lens {
implicit def category = new Category[Lens] {
def compose[A,B,C](g: Lens[B,C], f: Lens[A,B]): Lens[A,C] = f andThen g
def id[A]: Lens[A,A] = self[A]
}
implicit def asState[A,B](lens: Lens[A,B]) = lens.toState
implicit def invariantFunctor[A] = new InvariantFunctor[PartialApply1Of2[Lens,A]#Apply] {
def xmap[B,C](lens: Lens[A,B], f: B => C, g: C => B) = lens.xmap[C](f)(g)
}
implicit def generalizedFunctor[A] = new Category.GeneralizedFunctor[Lens,Function1,Id] {
def fmap[A,B](lens: Lens[A,B]) = lens.apply
}
def fst[A,B] = Lens[(A,B),A](_._1, (ab,a) => (a,ab._2))
def snd[A,B] = Lens[(A,B),B](_._2, (ab,b) => (ab._1,b))
def self[A] = Lens[A,A](a => a, (_, a) => a)
def trivial[A] = Lens[A,Unit](_ => Unit, (a, _) => a)
def codiag[A] : Lens[Either[A,A],A] = self[A] ||| self[A]
implicit def subtractableLens[S,A,Repr <: Subtractable[A,Repr]] = SubtractableLens[S,A,Repr](_)
case class SubtractableLens[S,A,Repr <: Subtractable[A, Repr]](lens: Lens[A,Repr]) {
def -= (elem: A) = lens.mods (_ - elem)
def -= (elem1: A, elem2: A, elems: A*) = lens.mods (_ - elem1 - elem2 -- elems)
def --= (xs: TraversableOnce[A]) = lens.mods (_ -- xs)
}
implicit def addableLens[S,A,Repr <: Addable[A,Repr]] = AddableLens[S,A,Repr](_)
case class AddableLens[S,A,Repr <: Addable[A, Repr]](lens: Lens[A,Repr]) {
def += (elem: A) = lens.mods (_ + elem)
def += (elem1: A, elem2: A, elems: A*) = lens.mods (_ + elem1 + elem2 ++ elems)
def ++= (xs: TraversableOnce[A]) = lens.mods (_ ++ xs)
}
implicit def setLikeLens[S,K,Repr <: SetLike[K,Repr] with Set[K]] = SetLikeLens[S,K,Repr](_)
case class SetLikeLens[S,K,Repr <: SetLike[K,Repr] with Set[K]](lens: Lens[S,Repr]) {
def contains(key: K) = Lens[S,Boolean](
s => lens(s).contains(key),
(s, b) => lens.mod(s, m => if (b) m + key else m - key)
)
def &= (that: Set[K]) = lens.mods(_ & that)
def &~=(that: Set[K]) = lens.mods(_ &~ that)
// def ^= (that: Set[K]) = lens.mods(_ ^ that)
def |= (that: Set[K]) = lens.mods(_ | that)
}
implicit def mapLens[S,K,V] = MapLens[S,K,V](_)
case class MapLens[S,K,V](lens: Lens[S,Map[K,V]]) {
def member(k:K) = Lens[S,Option[V]](
s => lens(s).get(k),
(s, opt) => lens.mod(s, m => opt.cata(v => m + (k -> v), m - k)))
// behavior undefined for accessing an element not present in the map!
def at(k:K) = Lens[S,V](lens.get(_)(k), (s, v) => lens.mod(s, _ + (k -> v)))
def +=(elem1: (K,V), elem2: (K,V), elems: (K,V)*) = lens.mods (_ + elem1 + elem2 ++ elems)
def +=(elem: (K, V)) = lens.mods (_ + elem)
def update(key: K, value: V) = lens.mods_(_.updated(key,value))
}
implicit def seqLens[S,A,Repr <: SeqLike[A,Repr]] = SeqLikeLens[S,A,Repr](_)
case class SeqLikeLens[S, A, Repr <: SeqLike[A,Repr]](lens: Lens[S,Repr]) {
def sortWith(lt: (A,A) => Boolean) = lens.mods_(_ sortWith lt)
def sortBy[B:math.Ordering](f:A=>B) = lens.mods_(_ sortBy f)
def sort[B >: A](implicit ord: math.Ordering[B]) = lens.mods_(_.sorted[B])
}
implicit def stackLens[S,A] = StackLens[S,A](_)
case class StackLens[S,A](lens: Lens[S,Stack[A]]) {
def push(elem1: A, elem2: A, elems: A*) = lens.mods_(_ push elem1 push elem2 pushAll elems)
def push(elem: A) = lens.mods_(_ push elem)
def pop = lens.mods_(_.pop)
def pop2 = lens.modps(_.pop2.swap)
def top = lens.map(_.top)
def length = lens.map(_.length)
}
implicit def queueLens[S,A] = QueueLens[S,A](_)
case class QueueLens[S,A](lens: Lens[S,Queue[A]]) {
def enqueue(elem: A) = lens.mods_(_ enqueue elem)
def dequeue = lens.modps(_.dequeue.swap)
def length = lens.map(_.length)
}
implicit def arrayLens[S,A] = ArrayLens[S,Array[A]](_)
case class ArrayLens[S,A](lens: Lens[S,Array[A]]) {
def at(i : Int) = Lens[S,A](
s => lens(s)(i),
(s, v) => lens.mod(s, array => {
val copy = array.clone()
copy.update(i,v)
copy
})
)
def length = lens.map(_.length)
}
implicit def numericLens[S,N:Numeric] = NumericLens[S,N](_)
case class NumericLens[S,N](lens: Lens[S,N])(implicit num: Numeric[N]) {
def +=(that:N) = lens.mods(num.plus(_,that))
def -=(that:N) = lens.mods(num.minus(_,that))
def *=(that:N) = lens.mods(num.times(_,that))
}
implicit def fractionalLens[S,F:Fractional] = FractionalLens[S,F](_)
case class FractionalLens[S,F](lens: Lens[S,F])(implicit frac: Fractional[F]) {
def /=(that:F) = lens.mods(frac.div(_,that))
}
implicit def integralLens[S,I:Integral] = IntegralLens[S,I](_)
case class IntegralLens[S,I](lens: Lens[S,I])(implicit int: Integral[I]) {
def %=(that:I) = lens.mods(int.quot(_,that))
}
}
|