Skip to content

Commit

Permalink
#15 - Made generic lenses work using Generic but need to call Generi…
Browse files Browse the repository at this point in the history
…c.product before
  • Loading branch information
julien-truffaut committed Mar 11, 2014
1 parent 9a634b0 commit 3bfe9e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 12 additions & 3 deletions core/src/main/scala/monocle/thirdparty/HList.scala
@@ -1,7 +1,9 @@
package monocle.thirdparty

import monocle.{SimpleLens, Iso, Lens}
import shapeless.HList._
import shapeless._
import shapeless.ops.hlist.{ReplaceAt, At}


object hlist extends HListInstances
Expand All @@ -20,9 +22,16 @@ trait HListInstances {
hl => hl.head -> hl.tail.head
)

def first[S, A, L <: HList](implicit gen : Generic.Aux[S, L], nth : HListNthLensAux[L, shapeless.nat._0.N, A]) = at(shapeless.nat._0)

def at[S, A, L <: HList](n : Nat)(implicit gen : Generic.Aux[S, L], nth : HListNthLensAux[L, n.N, A]) =
SimpleLens[S, A](s => nth.get(gen.to(s)), (s, a) => gen.from(nth.set(gen.to(s))(a)) )
def first[S, A, L <: HList](implicit gen: Generic.Aux[S, L],
at: At.Aux[L, shapeless.nat._0.N, A],
replace: ReplaceAt.Aux[L, shapeless.nat._0.N, A, (A, L)]) = _at(shapeless.nat._0)



def _at[S, A, L <: HList](n : Nat)(implicit gen: Generic.Aux[S, L],
at: At.Aux[L, n.N, A],
replace: ReplaceAt.Aux[L, n.N, A, (A, L)]) =
SimpleLens[S, A](s => gen.to(s).at(n), (s, a) => gen.from(gen.to(s).updatedAt(n, a)) )

}
8 changes: 6 additions & 2 deletions examples/src/main/scala/monocle/HListExample.scala
Expand Up @@ -2,6 +2,7 @@ package monocle

import monocle.thirdparty.hlist._
import shapeless._
import shapeless.Generic._

object HListExample extends App {

Expand All @@ -18,9 +19,12 @@ object HListExample extends App {


case class Person(name : String, age : Int)
val julien = Person("Julien", 27)
val character = Person("Julien", 27)

println( first.get(julien) )
implicit val gen = Generic.product[Person]

println( first.get(character) )
println( first.set(character, "Roger"))



Expand Down

0 comments on commit 3bfe9e9

Please sign in to comment.