我正在将fs2 0.10.x版本迁移到1.0.0版本。
我们的代码fs2.Scheduler从fs2版本0.10.x开始使用。
我不知道新的fs2版本1.0.0中的等效项是什么。
我在这里浏览了迁移指南,但是找不到从0.10.x到1.0.0或类似版本的迁移指南。
提前致谢。
我正在与猫打交道,我想将其转换val x: State[A, B]为猫StateT[IO, A, B]。注意:IO来自猫的效果。
如何优雅地做到这一点?
有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对于客户来说如何处理错误意味着什么呢?
我使用创建嵌入式 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) 我想测量 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) 我最近将我的应用程序转换为继承猫的应用程序,如此处IOApp所述。我在该文档中读到:
\n\n\nTimer[IO] 依赖项已由 IOApp 提供,因此在 JVM 之上,\xe2\x80\x99 不再需要隐式 ExecutionContext 位于范围内
\n
但是,我正在与其他几个确实需要ExecutionContext. 是否有推荐的方式来获取此类应用程序?老好人import scala.concurrent.ExecutionContext.Implicits.global对所提供的东西玩得好吗Timer[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 …
我正在采用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) 我是猫和函数式编程的新手,我正在努力进行单元测试函数数据类型,如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) 我正在尝试向猫学习功能随机。这是大类中的方法:
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)
但不知道如何将其隐含在对象伴侣中。
cats-effect ×10
scala ×10
scala-cats ×7
fs2 ×2
apache-kafka ×1
applicative ×1
implicit ×1
scala-3 ×1
specs2 ×1
typeclass ×1