本文整理汇总了Scala中cats.data.Coproduct类的典型用法代码示例。如果您正苦于以下问题:Scala Coproduct类的具体用法?Scala Coproduct怎么用?Scala Coproduct使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Coproduct类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ProductKK
//设置package包名称以及导入依赖的类
package io.aecor.liberator.data
import cats.data.Coproduct
import cats.~>
import io.aecor.liberator.Algebra
import io.aecor.liberator.Algebra.Aux
import shapeless.Lazy
final case class ProductKK[M[_[_]], N[_[_]], F[_]](fa: M[F], ga: N[F]) {
def :&:[L[_[_]]](la: L[F]): ProductKK[L, ProductKK[M, N, ?[_]], F] =
ProductKK(la, this)
}
object ProductKK {
implicit def opsInstance[F[_[_]], G[_[_]], FOp[_], GOp[_]](
implicit f: Lazy[Aux[F, FOp]],
g: Lazy[Aux[G, GOp]]
): Aux[ProductKK[F, G, ?[_]], Coproduct[FOp, GOp, ?]] =
new Algebra[ProductKK[F, G, ?[_]]] {
override type Out[A] = Coproduct[FOp, GOp, A]
override def toFunctionK[A[_]](of: ProductKK[F, G, A]): Coproduct[FOp, GOp, ?] ~> A =
?[Coproduct[FOp, GOp, ?] ~> A](
_.fold(f.value.toFunctionK(of.fa), g.value.toFunctionK(of.ga))
)
override def fromFunctionK[A[_]](fa: Coproduct[FOp, GOp, ?] ~> A): ProductKK[F, G, A] =
ProductKK(
f.value.fromFunctionK(?[FOp ~> A](x => fa(Coproduct.leftc(x)))),
g.value.fromFunctionK(?[GOp ~> A](x => fa(Coproduct.rightc(x))))
)
}
class ProductKKIdOps[F[_[_]], A[_]](val self: F[A]) extends AnyVal {
def :&:[G[_[_]]](ga: G[A]): ProductKK[G, F, A] = ProductKK(ga, self)
}
trait ProductKKSyntax {
implicit def toProductKKSyntax[F[_[_]], A[_]](self: F[A]): ProductKKIdOps[F, A] =
new ProductKKIdOps(self)
}
}
示例2: foldCP_
//设置package包名称以及导入依赖的类
package tierney.core
import cats.~>
import cats.data.Coproduct
trait CoproductSupport {
def foldCP_[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): Coproduct[F, G, ?] ~> H = Lambda[Coproduct[F, G, ?] ~> H](_.fold(f, g))
def left_[F[_], G[_]]: F ~> Coproduct[F, G, ?] = Lambda[F ~> Coproduct[F, G, ?]](Coproduct.leftc(_))
def right_[F[_], G[_]]: G ~> Coproduct[F, G, ?] = Lambda[G ~> Coproduct[F, G, ?]](Coproduct.rightc(_))
def rightMap[F[_], G[_], H[_]](f: G ~> H): Coproduct[F, G, ?] ~> Coproduct[F, H, ?] =
Lambda[Coproduct[F, G, ?] ~> Coproduct[F, H, ?]](_.fold[Coproduct[F, H, ?]](left_[F, H], f andThen[Coproduct[F, H, ?]] right_[F, H]))
def rightMap_[F[_]] : FunctorK[Lambda[(G[_], A) => Coproduct[F, G, A]]] = new FunctorK[Lambda[(G[_], A) => Coproduct[F, G, A]]] {
override def map[G[_], H[_]](f: G ~> H) = rightMap(f)
}
}
object CoproductSupport extends CoproductSupport
示例3: node
//设置package包名称以及导入依赖的类
package tierney.free
import cats.free.Free
import cats.Applicative
import tierney.core._
import CoproductSupport._
import FreeApplicativeSupport._
import FreeSupport._
import cats.data.Coproduct
import cats.~>
import cats.arrow.FunctionK
import cats.Monad
import tierney.parallel.ParallelApplicative
import cats.free.FreeApplicative
trait TierneyFree[F[_], A] extends Any {
def node: Node[F, A] = Node.right(serial)
def parallel: Parallel[F, A] = FreeApplicative.lift[Node[F, ?], A](node)
def serial: Serial[F, A] = Free.liftF[Parallel[F, ?], A](parallel)
final def compile[G[_]](f: F ~> G): Node[G, A] = functorKNode.map(f).apply(node)
private[this] def run(implicit mo: Monad[F], ap: Applicative[F]): F[A] =
node.cata[IdKK](
foldCP_[F, Free[FreeApplicative[F, ?], ?], F](
FunctionK.id,
foldMapF_[FreeApplicative[F, ?], F](fold_(ap)))
)(NodeSerialParallelF.functorKKNodeSerialParallelF)
final def runSerialOrUnprincipled(implicit mo: Monad[F]): F[A] = run
final def runSerialOrUnprincipled[G[_]](f: F ~> G)(implicit mo: Monad[G]): G[A] = compile(f).runSerialOrUnprincipled
final def runParallel(implicit mo: Monad[F], pa: ParallelApplicative[F]): F[A] = run(mo, pa)
final def runParallel[G[_]](f: F ~> G)(implicit mo: Monad[G], pa: ParallelApplicative[G]): G[A] = compile(f).runParallel
}
示例4: GameEngine
//设置package包名称以及导入依赖的类
package me.panavtec.tictactoe
import cats.data.Coproduct
import cats.free.Free
import cats.{Id, ~>}
import me.panavtec.tictactoe.Domain.{Board, Coordinate, Mark}
object GameEngine {
type PlayerInput = (Coordinate, Mark)
type GameState = (Mark, Board)
type TicTacToeApp[A] = Coproduct[Console, Game, A]
val interpreter: TicTacToeApp ~> Id = Console.interpreter or Game.interpreter
val I: ConsoleOps[TicTacToeApp] = implicitly[ConsoleOps[TicTacToeApp]]
val D: GameOps[TicTacToeApp] = implicitly[GameOps[TicTacToeApp]]
import D._
import I._
def init: GameState = startTicTacToe.flatMap(initialGameState =>
iterateUntil(Game.hasFinished)(initialGameState, turn)
).foldMap(interpreter)
def startTicTacToe: Free[TicTacToeApp, GameState] = {
for {
_ <- printLine("== Game Starts ==")
gameState <- startGame
} yield gameState
}
def turn(gameState: GameState): Free[TicTacToeApp, GameState] = {
val (player: Mark, board: Board) = gameState
for {
_ <- printLine(s"Turn Of Player ${Mark.stringRepresentation(player)}. Input next move [X,Y]:")
coordinate <- readLine
newGameState <- playNext(board, (coordinate, player))
_ <- printLine(Board.stringRepresentation(newGameState._2))
} yield newGameState
}
def iterateUntil[A[_], S](pred: S => Boolean)(initialState: S, next: S => Free[A, S]): Free[A, S] = {
next(initialState).flatMap(nextState =>
if (pred(nextState)) Free.pure(nextState)
else iterateUntil(pred)(nextState, next)
)
}
}