Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example uses scalaz.Lens's modf, modp and xmap

There are number of great tutorials and posts out there covering the more straightforward of Lens's methods, e.g. Cleaner way to update nested structures; can anyone provide example uses for these three other methods? Thanks.

like image 547
Joffer Avatar asked Apr 09 '13 16:04

Joffer


1 Answers

Unfortunately, Scalaz7 lens examples are a WIP. You need to ask this question of the Scalaz Google Group. Before you ask, try these examples here and watch Emmett's videos.

  • Using lenses with Scalaz 7
  • Emmett's videos on Lenses

Look at the source code again. What can you puzzle out from this?

 def xmapbA[X, A >: A2 <: A1](b: Bijection[A, X]): LensFamily[X, X, B1, B2] =
    xmapA(b to _)(b from _)

  def xmapB[X1, X2](f: B1 => X1)(g: X2 => B2): LensFamily[A1, A2, X1, X2] =
    lensFamily(a => run(a).xmap(f)(g))

  def xmapbB[X, B >: B1 <: B2](b: Bijection[B, X]): LensFamily[A1, A2, X, X] =
    xmapB(b to _)(b from _)


  /** Modify the value viewed through the lens, returning a functor `X` full of results. */
  def modf[X[+_]](f: B1 => X[B2], a: A1)(implicit XF: Functor[X]): X[A2] = {
    val c = run(a)
    XF.map(f(c.pos))(c put _)
  }

Sorry for the minimal help. I can just point at whom to ask and what you need to know before you ask.

like image 80
Raahul Kumar Avatar answered Sep 30 '22 20:09

Raahul Kumar