我最近将我的应用程序转换为继承猫的应用程序,如此处IOApp所述。我在该文档中读到:
\n\n\nTimer[IO] 依赖项已由 IOApp 提供,因此在 JVM 之上,\xe2\x80\x99 不再需要隐式 ExecutionContext 位于范围内
\n
但是,我正在与其他几个确实需要ExecutionContext. 是否有推荐的方式来获取此类应用程序?老好人import scala.concurrent.ExecutionContext.Implicits.global对所提供的东西玩得好吗Timer[IO]?
我正在尝试在 REPL 中运行 scala cats。按照 cat 的指示,我安装了 ammonite REPL 并将以下导入放入predef.sc
nterp.configureCompiler(_.settings.YpartialUnification.value = true)
import $ivy.`org.typelevel::cats-core:2.2.0-M1`, cats.implicits._
Run Code Online (Sandbox Code Playgroud)
我在运行时遇到此错误amm。
predef.sc:1: value YpartialUnification is not a member of scala.tools.nsc.Settings
val res_0 = interp.configureCompiler(_.settings.YpartialUnification.value = true)
^
Compilation Failed
Run Code Online (Sandbox Code Playgroud) 我有以下代码片段:
(for {
_ <- LiveUserQuery.make(DbManager.failRollback).create(user)
- <- IO.sleep(2.seconds)
a <- router.run(Request(GET, uri"/user/d85ec250-bb5c-11ea-b3de-0242ac130030")).value
} yield a).flatMap {
case Some(req) =>
req.as[User].map { u =>
val is_uuid_valid = u.id.compareTo(UUID.fromString("d85ec250-bb5c-11ea-b3de-0242ac130030")) == 0
expect(is_uuid_valid) && expect(u.gender == "F")
}
case None => expect(false)
}
Run Code Online (Sandbox Code Playgroud)
并想避免flatMap之后for。如何将代码块从 移动flatMap到for?
A 和我正在与 circe 一起做一些工作来编码/解码一些 ADT,我们遇到了一些我们根本不了解的功能。circe 文档中给出的示例按预期工作,但在深入研究后 - 不清楚解码示例为何有效,因此我们很难推理如何在需要时进行修改。
功能(来自关于 ADT 的 Circe 示例):
import cats.syntax.functor._
import io.circe.{ Decoder, Encoder }, io.circe.generic.auto._
import io.circe.syntax._
object GenericDerivation {
// Encoder Redacted
implicit val decodeEvent: Decoder[Event] =
List[Decoder[Event]](
Decoder[Foo].widen,
Decoder[Bar].widen,
Decoder[Baz].widen,
Decoder[Qux].widen
).reduceLeft(_ or _)
}
Run Code Online (Sandbox Code Playgroud)
我明白了 - 基本上从这个列表中选择第一个有效的解码器 - 有道理但是(!)
or似乎是一个采用按名称参数的一元函数。类型签名是:
final def or[AA >: A](d: => Decoder[AA]): Decoder[AA]
并且reduceLeft(来自 IterableOnce)需要一个二元函数。类型签名是:
def reduceLeft[B >: A](op: (B, A) => B): B
然后我的脑袋爆炸了。我显然错过了一些东西,无法弄清楚。
该示例绝对适用于转换 ADT。鉴于该or函数似乎没有满足所需的类型,为什么/如何工作reduceLeft?
Parallel我注意到,即使在最强大的 typeclass 中,cats-effect 类型类层次结构也不会从 cats 核心继承ConcurrentEffect。仅当直接使用 IO 时,才存在为并行提供的唯一实例。
但不应该有一个吗?我有点觉得Sync[F]并且Async[F]应该是一个很好的二人组Parallel[F]。
我将通过https://www.scala-exercises.org/获取Cats。我想我明白是什么意思了Apply.ap。但我看不到它有任何用途。
有什么区别:
Apply[Option].map(Some(1))(intToString)
Run Code Online (Sandbox Code Playgroud)
和
Apply[Option].ap(Some(intToString))(Some(1))
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下或指出更多解释吗?
我正在研究 Cats 以完成上述任务。我试着写下面的例子
import cats.effect.{IO, Async}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
val apiCall = Future.successful("I come from the Future!")
val ioa: IO[String] =
Async[IO].async { cb =>
import scala.util.{Failure, Success}
Thread.sleep(1000)
throw new RuntimeException
apiCall.onComplete {
case Success(value) => cb(Right(value))
case Failure(error) => cb(Left(error))
}
}
ioa.unsafeRunAsync(result => result match {
case Left(result) => throw result
case Right(_) =>
})
Run Code Online (Sandbox Code Playgroud)
然而,它似乎在这两件事上都失败了。它阻塞主线程,只在子线程中抛出异常。我正在尝试做的可能吗?
我有一个类型的值List[EitherT[IO, String, Int]],我想对其进行序列以便将其映射到EitherT[IO,String, List[Int]]
我阅读并找到了序列方法,但它给了我一个错误,说它需要 [G] 的隐式应用,如何解决这个问题
Let us say we have the code (<: Monad[F] doesn't work as expected):
class External[F[_] <: Monad[F] : Concurrent](implicit proxy: Proxy[F]) { ... }
class Proxy[F[_] <: Monad[F]](implicit storage: Storage, async: Async[F]) {
def get(key: String): F[Option[Entry]] = {
async.blocking(storage.get(key))
}
}
Run Code Online (Sandbox Code Playgroud)
I would like F[_] to be a Monad, so that proxy.get() have those traits and enables for example (inside External class):
proxy.get(key).flatMap(...)
Run Code Online (Sandbox Code Playgroud)
So far so good, but when trying to instantiate with cats.effect.IO it doesn't work …
从 2017 年有关 nanopass 编译器的演讲 ( https://github.com/sellout/recursion-scheme-talk/blob/master/nanopass-compiler-talk.org ) 中,我找到了下面的代码片段。在这段代码片段中,我看到两个通用约束,我已经上下搜索以理解它们,但无法找到有关它们的任何信息。我希望了解的是:
\nfinal case class Let[A](bindings: List[(String, A)], body: A)\nfinal case class If[A](test: A, consequent: A, alt: A)\n\ndef expandLet[Lambda :<: F]: Fix[Let :+: F] => Fix[F] =\n _.unFix match {\n case Let(bindings, body) =>\n bindings.unzip((names, exprs) =>\n Fix(App(Fix(Lam(names, expandLet(body)).inject),\n exprs.map(expandLet)).inject))\n // and don\xe2\x80\x99t forget the other cases\n }\n\ndef expandIf[Lambda :<: F]: Fix[If :+: F] => Fix[F] =\n _.unFix match {\n case If(test, …Run Code Online (Sandbox Code Playgroud) scala higher-kinded-types scalaz scala-cats recursion-schemes
scala ×10
scala-cats ×10
applicative ×2
cats-effect ×2
ammonite ×1
arity ×1
circe ×1
functor ×1
implicit ×1
methods ×1
scalaz ×1
sequence ×1
typeclass ×1
types ×1