Skip to content

Commit

Permalink
Added additional functions for instantiating using product instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalin-Rudnicki committed May 5, 2024
1 parent 8eba2d3 commit bee64da
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harness.deriving

import cats.syntax.option.*
import cats.syntax.traverse.*
import scala.compiletime.*
import scala.deriving.Mirror
import scala.reflect.ClassTag
Expand Down Expand Up @@ -74,6 +75,26 @@ abstract class K0T[UB] {

}

object mapInstantiate {

def apply(f: [t <: UB] => T[t] => t): F =
instantiate(instances.map { i => f(i).asInstanceOf[m.MirroredMonoType] })

def withLabels(labels: Labelling[F])(f: [t <: UB] => (String, T[t]) => t): F =
instantiate(labels.elemLabels.zip(instances).map { case (l, i) => f(l, i).asInstanceOf[m.MirroredMonoType] })

}

object mapInstantiateEither {

def apply[L](f: [t <: UB] => T[t] => Either[L, t]): Either[L, F] =
instances.traverse { i => f(i).asInstanceOf[Either[L, m.MirroredMonoType]] }.map(instantiate)

def withLabels[L](labels: Labelling[F])(f: [t <: UB] => (String, T[t]) => Either[L, t]): Either[L, F] =
labels.elemLabels.zip(instances).traverse { case (l, i) => f(l, i).asInstanceOf[Either[L, m.MirroredMonoType]] }.map(instantiate)

}

}

}
Expand Down

0 comments on commit bee64da

Please sign in to comment.