scala - How to implement parametric lenses that change type of state -
so in scala have typical lens signature as:
case class lens[o,v](get: o => v, set: (o,v) => o)
but can see, updates , sets values of same type, not set 1 type another. have in mind more this:
case class lens[o[_],a,b](get: o[a] => a, set: (o[a],b) => o[b])
with a
, b
make sense o[_]
my question is. stop being isomorphic? there simpler way without breaking rules?
i think figure out right lens
abstraction, helpful have concrete lens-able type in mind.
however, particular example, there can say:
case class lens[o[_],v[_],a,b](get: o[a] => v[a], set: (o[a],v[b]) => o[b])
i not think kind of lens can composed. in order compose lenses, result of get
has able feed set
. here, result of get
v[_]
, whereas set
needs o[_]
.
as further explanation, here possible kind of polymorphic lens, not 1 fits needs:
trait lens[t[_]] { def get[a](t: t[a]): def set[a,b](t: t[a], x: b): t[b] }
it can composed so:
def composelenses[t[_],u[_]](lens1: lens[t], lens2: lens[u]) = new lens[({type x[a] = t[u[a]]})#x] { def get[a](t: t[u[a]]): = lens2.get(lens1.get(t)) def set[a,b](t: t[u[a]], x: b): t[u[b]] = lens1.set(t, lens2.set(lens1.get(t), x)) }
i wouldn't have been able figure out definition of lens
abstractly -- in order had use concrete case:
case class box[a](x: a) def boxboxget[a](b: box[box[a]]): = ??? def boxboxset[a,b](b: box[box[a]], x: b): box[box[b]] = ???
Comments
Post a Comment