Skip to content

Commit

Permalink
Added RepresentableK instances with hand implementation without macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grryum committed Dec 4, 2023
1 parent c3adf17 commit bd1411b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
1 change: 0 additions & 1 deletion .java-version

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package tofu.concurrent

trait AtomInstances
import cats.~>
import tofu.higherKind.{RepresentableK, RepK}

trait AtomInstances {
// TODO: use higherKind.derived macro when it is ready for scala 3
given representableKInstance[A]: RepresentableK[({ type L[x[_]] = Atom[x, A] })#L] =
new RepresentableK[({ type L[x[_]] = Atom[x, A] })#L] {
def tabulate[F[_]](hom: RepK[({ type L[x[_]] = Atom[x, A] })#L, _] ~> F): Atom[F, A] = new Atom[F, A] {
def get: F[A] = hom(RepK[({ type L[x[_]] = Atom[x, A] })#L](_.get))
def set(a: A): F[Unit] = hom(RepK[({ type L[x[_]] = Atom[x, A] })#L](_.set(a)))
def getAndSet(a: A): F[A] = hom(RepK[({ type L[x[_]] = Atom[x, A] })#L](_.getAndSet(a)))
def update(f: A => A): F[Unit] = hom(RepK[({ type L[x[_]] = Atom[x, A] })#L](_.update(f)))
def modify[B](f: A => (A, B)): F[B] = hom(RepK[({ type L[x[_]] = Atom[x, A] })#L](_.modify(f)))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package tofu.concurrent

trait QVarInstances {}
import cats.~>
import tofu.higherKind.{RepresentableK, RepK}

trait QVarInstances {
// TODO: use higherKind.derived macro when it is ready for scala 3
given representableK[A]: RepresentableK[({ type L[x[_]] = QVar[x, A] })#L] =
new RepresentableK[({ type L[x[_]] = QVar[x, A] })#L] {
def tabulate[F[_]](hom: RepK[({ type L[x[_]] = QVar[x, A] })#L, _] ~> F): QVar[F, A] = new QVar[F, A] {
def isEmpty: F[Boolean] = hom(RepK[({ type L[x[_]] = QVar[x, A] })#L](_.isEmpty))
def put(a: A): F[Unit] = hom(RepK[({ type L[x[_]] = QVar[x, A] })#L](_.put(a)))
def take: F[A] = hom(RepK[({ type L[x[_]] = QVar[x, A] })#L](_.take))
def read: F[A] = hom(RepK[({ type L[x[_]] = QVar[x, A] })#L](_.read))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package tofu.generate

trait GenUUIDInstances {}
import cats.~>
import tofu.higherKind.{RepresentableK, RepK}
import java.util.UUID

trait GenUUIDInstances {
// TODO: use higherKind.derived macro when it is ready for scala 3
given genUUIDRepresentableK: RepresentableK[GenUUID] = new RepresentableK[GenUUID] {
def tabulate[F[_]](hom: RepK[GenUUID, _] ~> F): GenUUID[F] = new GenUUID[F] {
def randomUUID: F[UUID] = hom(RepK[GenUUID](_.randomUUID))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
package tofu.generate

trait GetRandomInstances {}
import cats.~>
import tofu.higherKind.{RepresentableK, RepK}

trait GetRandomInstances {
// TODO: use higherKind.derived macro when it is ready for scala 3
given genRandomRepresentableK: RepresentableK[GenRandom] = new RepresentableK[GenRandom] {
def tabulate[F[_]](hom: RepK[GenRandom, _] ~> F): GenRandom[F] = new GenRandom[F] {
def nextLong: F[Long] = hom(RepK[GenRandom](_.nextLong))
def nextInt(n: Int): F[Int] = hom(RepK[GenRandom](_.nextInt(n)))
}
}

}

0 comments on commit bd1411b

Please sign in to comment.