Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use eager parameters when by-name is not necessary #8

Merged
merged 1 commit into from
May 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object Eq {
[t] => (acc: Boolean, eqt: Eq[t], t0: t, t1: t) => Complete(!eqt.eqv(t0, t1))(false)(true)
)

given eqGenC[A](using inst: => K0.CoproductInstances[Eq, A]): Eq[A] with
given eqGenC[A](using inst: K0.CoproductInstances[Eq, A]): Eq[A] with
def eqv(x: A, y: A): Boolean = inst.fold2(x, y)(false)(
[t] => (eqt: Eq[t], t0: t, t1: t) => eqt.eqv(t0, t1)
)
Expand Down Expand Up @@ -117,15 +117,15 @@ object Ord {

def compare(x: String, y: String): Int = x.compare(y)

given ordGen[A](using inst: => K0.ProductInstances[Ord, A]): Ord[A] with
given ordGen[A](using inst: K0.ProductInstances[Ord, A]): Ord[A] with
def compare(x: A, y: A): Int = inst.foldLeft2(x, y)(0: Int)(
[t] => (acc: Int, ord: Ord[t], t0: t, t1: t) => {
val cmp = ord.compare(t0, t1)
Complete(cmp != 0)(cmp)(acc)
}
)

given ordGenC[A](using inst: => K0.CoproductInstances[Ord, A]): Ord[A] with
given ordGenC[A](using inst: K0.CoproductInstances[Ord, A]): Ord[A] with
def compare(x: A, y: A): Int = inst.fold2(x, y)((x: Int, y: Int) => x - y)(
[t] => (ord: Ord[t], t0: t, t1: t) => ord.compare(t0, t1)
)
Expand All @@ -147,7 +147,7 @@ object Functor {
given [F[_], G[_]](using ff: Functor[F], fg: Functor[G]): Functor[[t] =>> F[G[t]]] with
def map[A, B](fga: F[G[A]])(f: A => B): F[G[B]] = ff.map(fga)(ga => fg.map(ga)(f))

given functorGen[F[_]](using inst: => K1.Instances[Functor, F]): Functor[F] with
given functorGen[F[_]](using inst: K1.Instances[Functor, F]): Functor[F] with
def map[A, B](fa: F[A])(f: A => B): F[B] = inst.map(fa)([t[_]] => (ft: Functor[t], ta: t[A]) => ft.map(ta)(f))

given [T]: Functor[Const[T]] with
Expand Down Expand Up @@ -221,7 +221,7 @@ object Traverse {
def traverse[G[_], A, B](fa: Const[X][A])(f: A => G[B])(using G: Applicative[G]): G[Const[X][B]] =
G.pure(fa)

given traverseGen[F[_]](using inst: => K1.Instances[Traverse, F], func: K1.Instances[Functor, F]): Traverse[F] with
given traverseGen[F[_]](using inst: K1.Instances[Traverse, F], func: K1.Instances[Functor, F]): Traverse[F] with
import Functor.functorGen as delegate

def map[A, B](fa: F[A])(f: A => B): F[B] = delegate[F].map(fa)(f)
Expand Down Expand Up @@ -254,7 +254,7 @@ object Foldable {

def foldRight[A, B](fa: Const[X][A])(lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb

given foldableProduct[F[_]](using inst: => K1.ProductInstances[Foldable, F]): Foldable[F] with
given foldableProduct[F[_]](using inst: K1.ProductInstances[Foldable, F]): Foldable[F] with
def foldLeft[A, B](fa: F[A])(b: B)(f: (B, A) => B): B =
inst.foldLeft[A, B](fa)(b)(
[t[_]] => (acc: B, fd: Foldable[t], t0: t[A]) => Continue(fd.foldLeft(t0)(acc)(f))
Expand All @@ -266,7 +266,7 @@ object Foldable {
Continue(Eval.defer(fd.foldRight(t0)(acc)(f)))
)

given foldableCoproduct[F[_]](using inst: => K1.CoproductInstances[Foldable, F]): Foldable[F] with
given foldableCoproduct[F[_]](using inst: K1.CoproductInstances[Foldable, F]): Foldable[F] with
def foldLeft[A, B](fa: F[A])(b: B)(f: (B, A) => B): B =
inst.fold[A, B](fa)(
[t[_]] => (fd: Foldable[t], t0: t[A]) => fd.foldLeft(t0)(b)(f)
Expand All @@ -292,7 +292,7 @@ object FunctorK {
given [T]: FunctorK[K11.Id[T]] with
def mapK[A[_], B[_]](at: A[T])(f: A ~> B): B[T] = f(at)

given functorKGen[H[_[_]]](using inst: => K11.Instances[FunctorK, H]): FunctorK[H] with
given functorKGen[H[_[_]]](using inst: K11.Instances[FunctorK, H]): FunctorK[H] with
def mapK[A[_], B[_]](ha: H[A])(f: A ~> B): H[B] =
inst.map(ha)([t[_[_]]] => (ft: FunctorK[t], ta: t[A]) => ft.mapK(ta)(f))

Expand Down Expand Up @@ -326,7 +326,7 @@ object Bifunctor {
case Right(b) => Right(g(b))
}

given bifunctorGen[F[_, _]](using inst: => K2.Instances[Bifunctor, F]): Bifunctor[F] with
given bifunctorGen[F[_, _]](using inst: K2.Instances[Bifunctor, F]): Bifunctor[F] with
def bimap[A, B, C, D](fab: F[A, B])(f: A => C, g: B => D): F[C, D] =
inst.map(fab)([t[_, _]] => (bft: Bifunctor[t], tab: t[A, B]) => bft.bimap(tab)(f, g))

Expand Down Expand Up @@ -537,7 +537,7 @@ object Show {
}
}

given showGenC[T](using inst: => K0.CoproductInstances[Show, T]): Show[T] with {
given showGenC[T](using inst: K0.CoproductInstances[Show, T]): Show[T] with {
def show(t: T): String = inst.fold(t)([t] => (st: Show[t], t: t) => st.show(t))
}

Expand Down Expand Up @@ -620,7 +620,7 @@ object Read {
}
}

given readGenC[T](using inst: => K0.CoproductInstances[Read, T], labelling: Labelling[T]): Read[T] with {
given readGenC[T](using inst: K0.CoproductInstances[Read, T], labelling: Labelling[T]): Read[T] with {
def read(s: String): Option[(T, String)] = {
labelling.elemLabels.zipWithIndex.iterator.map((p: (String, Int)) => {
val (label, i) = p
Expand Down