Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Created September 7, 2012 16:32
Show Gist options
  • Save mpilquist/3667605 to your computer and use it in GitHub Desktop.
Save mpilquist/3667605 to your computer and use it in GitHub Desktop.
Trying to combine Lens[A, B] and Lens[A, C] in to Lens[A, (B, C)]
scala> def fanOut[A, B, C](f: Lens[A, B], g: Lens[A, C]): Lens[A, (B, C)] = Lens.lensu[A, (B, C)]({ case (a, (b, c)) => f.set(g.set(a, c), b) }, a => (f.get(a), g.get(a)))
fanOut: [A, B, C](f: scalaz.package.Lens[A,B], g: scalaz.package.Lens[A,C])scalaz.package.Lens[A,(B, C)]
scala> case class Foo(x: Int, y: Int)
defined class Foo
scala> val XL = Lens.lensu[Foo, Int]((f, x) => f.copy(x = x), _.x)
XL: scalaz.package.Lens[Foo,Int] = scalaz.LensTFunctions$$anon$5@3dfc8f84
scala> val YL = Lens.lensu[Foo, Int]((f, y) => f.copy(y = y), _.y)
YL: scalaz.package.Lens[Foo,Int] = scalaz.LensTFunctions$$anon$5@31b5d194
scala> val XYL = fanOut(XL, YL)
XYL: scalaz.package.Lens[Foo,(Int, Int)] = scalaz.LensTFunctions$$anon$5@4ce1e41f
scala> XYL.get(Foo(1, 2))
res2: (Int, Int) = (1,2)
scala> XYL.set(Foo(1, 2), (3, 4))
res3: scalaz.Id.Id[Foo] = Foo(3,4)
scala> implicit val fooEq = Equal.equalA[Foo]
fooEq: scalaz.Equal[Foo] = scalaz.Equal$$anon$4@6c6f070a
scala> XYL.lensLaw.identity(Foo(1, 2))
res11: Boolean = true
scala> XYL.lensLaw.retention(Foo(1, 2), (3, 4))
res12: Boolean = true
scala> XYL.lensLaw.doubleSet(Foo(1, 2), (0, 0), (3, 4))
res13: Boolean = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment