fancadescala/core/src/main/scala/tf/bug/fancadetagless/package.scala

43 lines
1.2 KiB
Scala

package tf.bug
import cats.data.StateT
import cats.effect.Sync
import cats.implicits._
import io.chrisdavenport.fuuid.FUUID
import scalax.collection.GraphEdge.DiHyperEdge
package object fancadetagless {
type FancadeT[F[_], A] = StateT[F, World, Pins[A]]
object FancadeT {
def make[F[_]](make: Make)(implicit s: Sync[F]): FancadeT[F, Unit] = {
val block = Block.fromMake[F](make)
StateT.apply { w =>
block.map { b =>
(w.copy(w.blocks + b), Pins(b.pins.toVector))
}
}
}
def constant[F[_], A](a: A)(implicit encoding: Encoding[A], s: Sync[F]): FancadeT[F, A] = {
val makes = encoding.encodeConstant(a)
val blocks = makes.traverse[F, Block](Block.fromMake[F])
StateT.apply { world =>
for {
b <- blocks
s = b.toSet
nw = world.copy(blocks = world.blocks ++ s)
} yield (nw, Pins(b.flatMap(_.pins)))
}
}
def connect[F[_]](from: Pin, to: Pin)(implicit s: Sync[F]): FancadeT[F, Unit] = {
StateT.apply { w =>
val newConn = DiHyperEdge(from, to)
s.pure(w.copy(connections = w.connections incl newConn), Pins(Vector.empty))
}
}
}
}