• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala Coproduct类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)
  }
} 
开发者ID:aecor,项目名称:liberator,代码行数:41,代码来源:ProductKK.scala


示例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 
开发者ID:m50d,项目名称:tierney,代码行数:18,代码来源:CoproductSupport.scala


示例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
} 
开发者ID:m50d,项目名称:tierney,代码行数:37,代码来源:TierneyFree.scala


示例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)
    )
  }
} 
开发者ID:PaNaVTEC,项目名称:TicTacToe,代码行数:49,代码来源:GameEngine.scala



注:本文中的cats.data.Coproduct类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala MultiNodeSpecCallbacks类代码示例发布时间:2022-05-23
下一篇:
Scala JsonMappingException类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap