本文整理汇总了Scala中cats.arrow.FunctionK类的典型用法代码示例。如果您正苦于以下问题:Scala FunctionK类的具体用法?Scala FunctionK怎么用?Scala FunctionK使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FunctionK类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: ATMTest
//设置package包名称以及导入依赖的类
import atlast.{ATM, Fx, InterpretersTo}
import cats.arrow.FunctionK
import cats.~>
import cats.implicits._
import org.scalatest.{FreeSpec, Matchers}
class ATMTest extends FreeSpec with Matchers {
"unions" - {
val noneMaker: Option ~> Option = new (Option ~> Option) {
override def apply[A](fa: Option[A]): Option[A] = fa.flatMap(_ => None)
}
def onlyRights[L]: (Either[L, ?] ~> Option) = new (Either[L, ?] ~> Option) {
override def apply[A](fa: L Either A): Option[A] = fa.right.toOption
}
def nonesThis[L](l: L): (Option ~> Either[L, ?]) = new (Option ~> Either[L, ?]) {
override def apply[A](fa: Option[A]): L Either A = fa.fold(Either.left[L, A](l))(Either.right(_))
}
"single member" in {
ATM.to[Fx.One[Option ~> ?[_]]](Option(1)).run(
InterpretersTo.one(noneMaker)
) should be (empty)
}
"two of the same member" in {
ATM.to[Fx.Two[Option ~> ?[_], Option ~> ?[_]]](Option(1)).run(
InterpretersTo.two(FunctionK.id[Option], noneMaker)
) should be (Some(1))
}
"two different members" in {
ATM.to[Fx.Two[Either[String, ?] ~> ?[_], Option ~> ?[_]]](Either.right[String, Int](1)).run(
InterpretersTo.two(onlyRights, noneMaker)
) should be (Some(1))
ATM.to[Fx.Two[Either[String, ?] ~> ?[_], Option ~> ?[_]]](None).run(
InterpretersTo.two(FunctionK.id, nonesThis("hey"))
) should be (Left("hey"))
}
}
}
开发者ID:edmundnoble,项目名称:atlast,代码行数:40,代码来源:ATLTest.scala
示例2: FreeMonadSpec
//设置package包名称以及导入依赖的类
package eleutheros
import cats.arrow.FunctionK
import cats.instances.option._
import cats.syntax.flatMap._
import cats.syntax.functor._
import org.scalatest.{FlatSpec, Matchers}
class FreeMonadSpec extends FlatSpec with Matchers {
"eleutheros" should "be possible to create a Free monad with iota's CopQ" in {
import FreeMonadIota._
import FreeMonad._
val one = FreeMonad.pure[Option, Int](1)
val two = FreeMonad.liftF[Option, Int](Some(2))
val f: Int => FreeMonad[Option, Int] = i => pure(i + 1)
val xyz = for {
x <- one // 1
y <- two // 2
z <- f(y) // 2 + 1 = 3
} yield x + y + z // 1 + 2 + 3 = 6
val result: Option[Int] = FreeMonad.foldMap(xyz)(FunctionK.id)
result should be (Some(6))
}
it should "be possible to create a Free monad with EitherF" in {
import FreeMonadEitherF._
import FreeMonad._
val one = FreeMonad.pure[Option, Int](1)
val two = FreeMonad.liftF[Option, Int](Some(2))
val f: Int => FreeMonad[Option, Int] = i => pure(i + 1)
val xyz = for {
x <- one // 1
y <- two // 2
z <- f(y) // 2 + 1 = 3
} yield x + y + z // 1 + 2 + 3 = 6
val result: Option[Int] = FreeMonad.foldMap(xyz)(FunctionK.id)
result should be (Some(6))
}
}
开发者ID:peterneyens,项目名称:eleutheros,代码行数:52,代码来源:FreeMonadSpec.scala
示例3: SeqParSpec
//设置package包名称以及导入依赖的类
package eleutheros
import cats.arrow.FunctionK
import cats.instances.option._
import cats.syntax.apply._
import cats.syntax.flatMap._
import cats.syntax.functor._
import org.scalatest.{FlatSpec, Matchers}
class SeqParSpec extends FlatSpec with Matchers {
"eleutheros" should "be possible to create a SeqPar with iota's CopQ" in {
import SeqParExample._
import SeqPar._
val one = SeqPar.pure[Option, Int](1)
val two = SeqPar.liftF[Option, Int](Some(2))
val f: Int => SeqPar[Option, Int] = i => SeqPar.pure(i + 1)
val xyz = for {
x <- one
y <- two
z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5
} yield x + y + z // 1 + 2 + 5 = 8
val result: Option[Int] = SeqPar.foldMap(xyz)(FunctionK.id)
result should be (Some(8))
}
it should "analyze" in {
import cats.~>
import cats.instances.list._
import cats.syntax.cartesian._
import SeqParExample._
import SeqPar._
sealed abstract class GetOp[A] extends Product with Serializable
case class Get(id: Int) extends GetOp[String]
val lift: Int => SeqPar[GetOp, String] =
i => SeqPar.liftF[GetOp, String](Get(i))
val a = (lift(1) |@| lift(2) |@| lift(3)).map(_ + _ + _)
val b = a.flatMap(s => lift(s.length))
val c = (lift(0) |@| b).map(_ + _)
val ids: GetOp ~> ?[? => List[Int]] =
?[GetOp ~> ?[? => List[Int]]]{ case Get(x) => x :: Nil }
SeqPar.analyze(a)(ids) should be (List(1, 2, 3))
SeqPar.analyze(b)(ids) should be (List(1, 2, 3))
SeqPar.analyze(c)(ids) should be (List(0, 1, 2, 3))
}
}
开发者ID:peterneyens,项目名称:eleutheros,代码行数:59,代码来源:SeqParSpec.scala
示例4: SeqParBench
//设置package包名称以及导入依赖的类
package eleutheros.bench
import cats.arrow.FunctionK
import cats.instances.option._
import cats.syntax.apply._
import cats.syntax.flatMap._
import cats.syntax.functor._
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}
@State(Scope.Benchmark)
class SeqParBench {
@Benchmark
def eleutherosIotaSeqPar: Option[Int] = {
import eleutheros.SeqParExample._
import SeqPar._
val one = SeqPar.pure[Option, Int](1)
val two = SeqPar.liftF[Option, Int](Some(2))
val f: Int => SeqPar[Option, Int] = i => SeqPar.pure(i + 1)
val xyz: SeqPar[Option, Int] = for {
x <- one // 1
y <- two // 2
z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5
} yield x + y + z // 1 + 2 + 5 = 8
val result: Option[Int] = SeqPar.foldMap(xyz)(FunctionK.id)
result
}
@Benchmark
def freestyleFreeS: Option[Int] = {
import cats.~>
import freestyle._
val one = FreeS.pure[Option, Int](1)
val two = FreeS.liftFA[Option, Int](Some(2))
val f: Int => FreeS[Option, Int] = i => FreeS.pure(i + 1)
val xyz = for {
x <- one // 1
y <- two // 2
z <- f(y).map2(two)(_ + _) // (2 + 1) + 2 = 5
} yield x + y + z // 1 + 2 + 5 = 8
implicit val fk: Option ~> Option = FunctionK.id[Option]
val result: Option[Int] = xyz.interpret[Option]
result
}
}
开发者ID:peterneyens,项目名称:eleutheros,代码行数:56,代码来源:SeqParBench.scala
示例5: 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
示例6: LoadedRepo
//设置package包名称以及导入依赖的类
package mco.config
import scala.util.Try
import cats.arrow.FunctionK
import cats.instances.try_._
import cats.instances.vector._
import cats.syntax.all._
import mco._
import mco.io.files.{IO, Path}
import mco.io.{FolderSource, IsolatedRepo}
import mco.persistency.JsonStorage
import mco.persistency.JsonStorage.Converters._
import rapture.json.jsonBackends.jawn._
object LoadedRepo {
private def classifier(c: RepoConfig): Try[Classifier[IO]] =
CompileAs[Classifier[IO]](c.classifier)
private def media(c: RepoConfig): Try[Vector[Media.Companion[IO]]] =
c.media traverse CompileAs[Media.Companion[IO]]
private def source(c: RepoConfig): IO[Source[IO]] =
IO.sequence(classifier(c) |@| media(c) map ((cls, ms) => FolderSource(c.source, cls, ms: _*)))
.absorbTry
private def repo(c: RepoConfig)(src: Source[IO]): IO[Repository[IO, Unit]] =
(c.kind, c.persistency) match {
case (RepoKind.Isolated, Persistency.JSON) =>
JsonStorage.preload(c.key, IsolatedRepo, Path(c.key + ".json"), c.target, src)
.map(new EffectRepo(_, FunctionK.id[IO]))
case (RepoKind.Isolated, Persistency.None) =>
IsolatedRepo(c.key, src, c.target, Set())
.map(new EffectRepo(_, FunctionK.id[IO]))
}
def apply(c: RepoConfig): IO[Repository[IO, Unit]] = source(c) flatMap repo(c)
}
开发者ID:oleg-py,项目名称:mco,代码行数:40,代码来源:LoadedRepo.scala
示例7: Behavior
//设置package包名称以及导入依赖的类
package aecor.data
import cats.arrow.FunctionK
import cats.data.StateT
import cats.implicits._
import cats.{ FlatMap, Functor, ~> }
final case class Behavior[F[_], Op[_]](run: Op ~> PairT[F, Behavior[F, Op], ?]) {
def mapK[G[_]: Functor](f: F ~> G): Behavior[G, Op] = Behavior[G, Op] {
def mk[A](op: Op[A]): PairT[G, Behavior[G, Op], A] =
f(run(op)).map {
case (b, a) =>
(b.mapK(f), a)
}
FunctionK.lift(mk _)
}
}
object Behavior {
def roll[F[_]: FlatMap, Op[_]](f: F[Behavior[F, Op]]): Behavior[F, Op] =
Behavior[F, Op](new (Op ~> PairT[F, Behavior[F, Op], ?]) {
override def apply[A](op: Op[A]): PairT[F, Behavior[F, Op], A] =
FlatMap[F].flatMap(f)(_.run(op))
})
def fromState[F[_]: FlatMap, Op[_], S](zero: S, f: Op ~> StateT[F, S, ?]): Behavior[F, Op] =
Behavior[F, Op](new (Op ~> PairT[F, Behavior[F, Op], ?]) {
override def apply[A](fa: Op[A]): PairT[F, Behavior[F, Op], A] =
f(fa).run(zero).map {
case (next, a) =>
fromState(next, f) -> a
}
})
def correlated[F[_], Op[_]](f: Op[_] => Behavior[F, Op]): Behavior[F, Op] =
Behavior(Lambda[Op ~> PairT[F, Behavior[F, Op], ?]] { op =>
f(op).run(op)
})
}
开发者ID:notxcain,项目名称:aecor,代码行数:41,代码来源:Behavior.scala
示例8: TestFree
//设置package包名称以及导入依赖的类
import cats.Id
import cats.arrow.FunctionK
import org.scalatest.{Matchers, FunSuite}
class TestFree extends FunSuite with Matchers {
test("Running") {
import si18n.core._
import si18n.core.ctx._
import si18n.free._
val x = 0
val y = 1
val a = i18n"$x $y"
val b = i18n"$x $y".compile(new FunctionK[I18n, Id] {
override def apply[A](fa: I18n[A]): Id[A] = (fa match {
case I18n.RawString(s) => s
case I18n.FormatInt(i) => i.toString
case I18n.Context(parts, vars) => parts.zipAll(vars, "", "").map(t => t._1 + t._2).mkString("")
}).asInstanceOf[A]
}).run
b should be("0 1")
}
}
开发者ID:alexknvl,项目名称:si18n,代码行数:27,代码来源:TestFree.scala
示例9: apply
//设置package包名称以及导入依赖的类
package effectful.augments
import cats._
import cats.arrow.FunctionK
// combine capture, natural transformation & monad
trait CaptureTransform[F[_],G[_]] {
def apply[A](f: => F[A]) : G[A]
}
object CaptureTransform {
implicit def apply[F[_],G[_]](implicit
C:Capture[G],
G:Monad[G],
K:FunctionK[F,G]
) : CaptureTransform[F,G] =
new CaptureTransform[F,G] {
override def apply[A](f: => F[A]): G[A] = {
G.flatMap(C.capture(K(f)))(identity)
}
}
}
开发者ID:lancegatlin,项目名称:effectful-demo,代码行数:23,代码来源:CaptureTransform.scala
示例10: ServicePML
//设置package包名称以及导入依赖的类
import scala.collection.generic.CanBuildFrom
import cats._
import cats.arrow.FunctionK
import effectful.augments._
import effectful.impl.AbstractComposedMonad
import scala.concurrent.Future
package object effectful {
// todo: figure out how this sugar is declared in emm
// type |:[F[_],G[_]] = F[G[_]]
// val |: = Nested
implicit class ServicePML[S[_[_]],F[_]](val self: S[F]) extends AnyVal {
def liftService[G[_]](implicit
X:CaptureTransform[F,G],
liftService:LiftService[S]
) : S[G] = liftService(self)
}
implicit def composedMonadFuture[G[_]](implicit
F: Monad[Future],
G: Monad[G],
GT: Traverse[G]
) : Monad[({ type FG[A] = Future[G[A]]})#FG] = new AbstractComposedMonad[Future,G] {
def tailRecM[A, B](a: A)(f: (A) => Future[G[Either[A, B]]]) : Future[G[B]] =
notStackSafeM(a)(f)
}
implicit def composedFunctionK[F[_],G[_]](implicit
F: Applicative[F]
) : FunctionK[G,({type FG[A]=F[G[A]]})#FG] = new FunctionK[G,({type FG[A]=F[G[A]]})#FG] {
def apply[A](fa: G[A]) = F.pure(fa)
}
}
开发者ID:lancegatlin,项目名称:effectful-demo,代码行数:37,代码来源:package.scala
注:本文中的cats.arrow.FunctionK类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论