diff --git a/kyo-core/shared/src/main/scala/kyo/edits.scala b/kyo-core/shared/src/main/scala/kyo/edits.scala new file mode 100644 index 000000000..a90226974 --- /dev/null +++ b/kyo-core/shared/src/main/scala/kyo/edits.scala @@ -0,0 +1,45 @@ +package kyo + +import kyo.sums._ +import kyo.ios._ + +object ttt extends KyoApp { + import edits._ + + case class Address(street: String) + case class Person(name: String, age: Int, address: Address) + + def run = + Edits(Person("test", 10, Address("rua")))(_[Address]("address").set(Address("rua 2"))) +} + +object edits { + + private val sums = Sums[Map[Edit[Any], Any]] + + class Edit[T](path: List[String], value: T) { + def apply[U](id: String): Edit[U] = + new Edit[U](id :: path, value.asInstanceOf[U]) + def get: T > Edits = + sums.get.map(_.getOrElse(this.asInstanceOf[Edit[Any]], value).asInstanceOf[T]) + def set(v: T): Unit > Edits = + sums.add(Map(this.asInstanceOf[Edit[Any]] -> v)).unit + + override def toString = s"Edit(${path.reverse.mkString(".")})" + } + + type Edits >: Edits.Effects <: Edits.Effects + + object Edits { + + type Effects = Sums[Map[Edit[Any], Any]] + + def apply[T, S](v: T)(f: Edit[T] => Unit > (Edits with S)): T > S = + sums.run(f(new Edit(Nil, v))).map { + case ((), edits) => + println(edits) + v + } + } + +} diff --git a/kyo-core/shared/src/main/scala/kyo/sums.scala b/kyo-core/shared/src/main/scala/kyo/sums.scala index 8240a89e8..715697c06 100644 --- a/kyo-core/shared/src/main/scala/kyo/sums.scala +++ b/kyo-core/shared/src/main/scala/kyo/sums.scala @@ -102,12 +102,13 @@ object sums { def init = _init def add(v1: V, v2: V) = _add(v1, v2) } - implicit val intSummer: Summer[Int] = Summer(0)(_ + _) - implicit val longSummer: Summer[Long] = Summer(0L)(_ + _) - implicit val doubleSummer: Summer[Double] = Summer(0d)(_ + _) - implicit val floatSummer: Summer[Float] = Summer(0f)(_ + _) - implicit val stringSummer: Summer[String] = Summer("")(_ + _) - implicit def listSummer[T]: Summer[List[T]] = Summer(List.empty[T])(_ ++ _) - implicit def setSummer[T]: Summer[Set[T]] = Summer(Set.empty[T])(_ ++ _) + implicit val intSummer: Summer[Int] = Summer(0)(_ + _) + implicit val longSummer: Summer[Long] = Summer(0L)(_ + _) + implicit val doubleSummer: Summer[Double] = Summer(0d)(_ + _) + implicit val floatSummer: Summer[Float] = Summer(0f)(_ + _) + implicit val stringSummer: Summer[String] = Summer("")(_ + _) + implicit def listSummer[T]: Summer[List[T]] = Summer(List.empty[T])(_ ++ _) + implicit def setSummer[T]: Summer[Set[T]] = Summer(Set.empty[T])(_ ++ _) + implicit def mapSummer[T, U]: Summer[Map[T, U]] = Summer(Map.empty[T, U])(_ ++ _) } }