Zipper update

27 views
Skip to first unread message

Peter Schmitz

unread,
May 28, 2015, 11:37:31 AM5/28/15
to shapel...@googlegroups.com
With Zipper I can get and put (overwrite) the element at the current cursor postion.
But how can I update this element with a function?
For example:

  case class SubModel(i: Int)
 
case class Model(sub: SubModel, s: String)
  val model
= Model(SubModel(0), "-")
  model
.toZipper.down.updateBy(_ + 1).root.reify // Model(SubModel(1),"-")

Thanks in advance,

Peter

Peter Schmitz

unread,
May 28, 2015, 12:59:59 PM5/28/15
to shapel...@googlegroups.com
My proposal:

  import shapeless._
 
import syntax.zipper._

 
case class SubModel(i: Int)
 
case class Model(sub: SubModel, s: String)


 
implicit class ZipperModify[C, L <: HList, R <: HList, P](private val zipper: Zipper[C, L, R, P]) extends AnyVal {
   
import shapeless.ops.zipper._
   
def modify[A, B](f: A => B)(implicit get: Get[zipper.Self] { type Out = A }, put: Put[zipper.Self, B]): put.Out = put(zipper, f(get(zipper)))

 
}

  val model
= Model(SubModel(0), "-")

  model
.toZipper.down.modify { i: Int => i + 1 }.root.reify // Model(SubModel(1),-)


Did I overlook something or can this be added to the Zipper API (without implicit class, of course)?

Miles Sabin

unread,
May 28, 2015, 1:51:06 PM5/28/15
to shapel...@googlegroups.com
Hi Peter,

The shapeless-dev list is deprecated in favour of either shapeless's
gitter channel,

https://gitter.im/milessabin/shapeless

or the typelevel mailing list,

https://groups.google.com/group/typelevel

You'll find the most activity by far on gitter ...

Cheers,


Miles
> --
> You received this message because you are subscribed to the Google Groups
> "Shapeless Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shapeless-de...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin
Reply all
Reply to author
Forward
0 new messages