标签: cats-effect

在较新版本的fs2(0.10.x)中等效于fs2.Scheduler的内容

我正在将fs2 0.10.x版本迁移到1.0.0版本。

我们的代码fs2.Scheduler从fs2版本0.10.x开始使用。

我不知道新的fs2版本1.0.0中的等效项是什么。

我在这里浏览了迁移指南但是找不到从0.10.x到1.0.0或类似版本的迁移指南。

提前致谢。

scala scala-cats fs2 cats-effect

3
推荐指数
1
解决办法
236
查看次数

如何从Cats State创建Cats IO monad

我正在与猫打交道,我想将其转换val x: State[A, B]为猫StateT[IO, A, B]。注意:IO来自猫的效果。
如何优雅地做到这一点?

scala scala-cats cats-effect

3
推荐指数
1
解决办法
106
查看次数

在哪里使用“ApplicativeError”而不是“Either”?

ApplicativeError[F,E]+F[A]并且有Either[E, A]。两者都传达了函数可能失败E或成功的消息,A但我不确定它们向客户端传达的关于处理错误的预期方式的不同消息:

def f1[F[_]: Applicative[F]]: F[Either[E, A]]
def f2[F[_]: ApplicativeError[F,E]]: F[A]
Run Code Online (Sandbox Code Playgroud)

我理解的f1意思是:客户端负责错误处理。但是f2对于客户来说如何处理错误意味着什么呢?

scala applicative scala-cats cats-effect

3
推荐指数
1
解决办法
498
查看次数

如何使用 fs2-kafka 读取嵌入式 kafka

我正在使用fs2-kafka来读取嵌入的 kafka

我使用创建嵌入式 kafka withRunningKafkaOnFoundPort,创建主题并发布一些消息。然而,当我尝试用 fs2-kafka 读回它时,我得到一个 NullPointerException。我已经隔离了一个测试用例,代码如下。

这是我的代码:

import cats.effect._
import cats.implicits._
import cats.effect.implicits._
import fs2.Stream
import fs2.kafka.{AutoOffsetReset, ConsumerSettings, KafkaConsumer, consumerStream}
import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig}
import org.scalatest.{BeforeAndAfterAll, FunSuite}

import scala.concurrent.ExecutionContext

class KafkaSuite extends FunSuite with EmbeddedKafka {

  val singleThreadExecutor = ExecutionContext.fromExecutor((task: Runnable) => task.run())
  implicit val contextShift = IO.contextShift(singleThreadExecutor)
  implicit val timer = IO.timer(singleThreadExecutor)

  val topic = "example"
  val partition = 0
  val clientId = "client"

  test("works") {
    val userDefinedConfig = EmbeddedKafkaConfig(kafkaPort = 0, zooKeeperPort …
Run Code Online (Sandbox Code Playgroud)

scala apache-kafka fs2 embedded-kafka cats-effect

3
推荐指数
1
解决办法
1286
查看次数

如何测量 Cats IO 效果中的经过时间?

我想测量 IO 容器内经过的时间。使用普通调用或期货相对容易(例如,类似于下面的代码)

class MonitoringComponentSpec extends FunSuite with Matchers with ScalaFutures {

  import scala.concurrent.ExecutionContext.Implicits.global

  def meter[T](future: Future[T]): Future[T] = {
    val start = System.currentTimeMillis()
    future.onComplete(_ => println(s"Elapsed ${System.currentTimeMillis() - start}ms"))
    future
  }

  def call(): Future[String] = Future {
    Thread.sleep(500)
    "hello"
  }

  test("metered call") {
    whenReady(meter(call()), timeout(Span(550, Milliseconds))) { s =>
      s should be("hello")
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但不确定如何包装 IO 调用

  def io_meter[T](effect: IO[T]): IO[T] = {
    val start = System.currentTimeMillis()
    ???
  }

  def io_call(): IO[String] = IO.pure {
    Thread.sleep(500)
    "hello"
  } …
Run Code Online (Sandbox Code Playgroud)

functional-programming scala scala-cats cats-effect

2
推荐指数
1
解决办法
874
查看次数

scala cats IOApp 应如何获取 ExecutionContext?

我最近将我的应用程序转换为继承猫的应用程序,如此IOApp所述。我在该文档中读到:

\n\n
\n

Timer[IO] 依赖项已由 IOApp 提供,因此在 JVM 之上,\xe2\x80\x99 不再需要隐式 ExecutionContext 位于范围内

\n
\n\n

但是,我正在与其他几个确实需要ExecutionContext. 是否有推荐的方式来获取此类应用程序?老好人import scala.concurrent.ExecutionContext.Implicits.global对所提供的东西玩得好吗Timer[IO]

\n

scala scala-cats cats-effect

2
推荐指数
1
解决办法
2115
查看次数

Make F[_] implementing Monad trait while accepting IO

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 …

scala implicit typeclass scala-cats cats-effect

2
推荐指数
1
解决办法
452
查看次数

Scala 使用猫 IO/Either 而不是 Future/Exceptions

我正在采用IO/在适用的情况下Either替换Future/ Exception,但我需要以下代码的帮助:

// some Java library
def dbLoad(id: Int): Int = {
  throw new Exception("db exception")
}

// my scala code
sealed trait DbError extends Exception with Product

object DbError {
  case object SomeError extends DbError
}

val load: Int => IO[Either[DbError, Int]] = { id =>
  IO.fromFuture { IO { Future {
    try { Right(dbLoad(id)) } catch { case NonFatal(e) => Left(SomeError) }
  } } }
}

val loadAll: IO[Either[DbError, (Int, Int, …
Run Code Online (Sandbox Code Playgroud)

scala cats-effect

2
推荐指数
1
解决办法
1548
查看次数

猫的匹配器EitherT和效果

我是猫和函数式编程的新手,我正在努力进行单元测试函数数据类型,如EitherT. 有示例代码:

class Library[F[_]]() {
    def create(book: Book)(implicit M: Monad[F]): EitherT[F, BookAlreadyExistsError, Book] = ...
}
Run Code Online (Sandbox Code Playgroud)

我想使用 Spec2 对其进行测试,但我不知道如何正确进行。尝试过这样的事情,但它不起作用:

  val library = Library[IO]()

  test("create book") {

    val book = Book("Title 1", 2016, "author 1")
    (for (
      resultBook <- library.create(book)
    ) yield resultBook shouldEqual ???
    ).unsafeRunSync()

  }
Run Code Online (Sandbox Code Playgroud)

我想有这样的非常简单的断言:

   resultBook shouldEqual Right(Book("Title 1", 2016, "author 1"))
   // or
   resultBook shouldEqual Left(BookAlreadyExistsError)
Run Code Online (Sandbox Code Playgroud)

functional-programming scala specs2 scala-cats cats-effect

1
推荐指数
1
解决办法
335
查看次数

Scala 猫.随机

我正在尝试向猫学习功能随机。这是大类中的方法:

def prepareDate(order: Model, zoneId: String): F[Instant] =
    Random[F].betweenLong(7200, 21600).flatMap { seconds =>
      order.dateTime
        .plusSeconds(seconds)
        .toInstant(ZoneOffset.of(zoneId))
        .asInstanceOf[F[Instant]]
    }
Run Code Online (Sandbox Code Playgroud)

它编译时不会No given instance of type cats.effect.std.Random[F] was found for parameter ev of method apply in object Random.出错

我尝试以这种方式将 Random 放入其中:

class SomeClass[F[_]: Async: Logger: Random]
Run Code Online (Sandbox Code Playgroud)

但不知道如何将其隐含在对象伴侣中。

scala cats-effect scala-3

1
推荐指数
1
解决办法
94
查看次数