Skip to content

Commit

Permalink
experiment: edits
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Dec 16, 2023
1 parent 9ae6418 commit 94b04e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
45 changes: 45 additions & 0 deletions kyo-core/shared/src/main/scala/kyo/edits.scala
Original file line number Diff line number Diff line change
@@ -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
}
}

}
15 changes: 8 additions & 7 deletions kyo-core/shared/src/main/scala/kyo/sums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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])(_ ++ _)
}
}

0 comments on commit 94b04e0

Please sign in to comment.