问题标签 [cats-effect]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
217 浏览

scala - 从 monix Scheduler 上执行的所有任务中收集数据

我正在使用 Monix Scheduler 定期执行一些任务。但我不知道如何不仅执行它们,还从它们收集结果到某个集合......假设我有一个计划任务,每次都返回一个随机数:

从理论上讲,我可以在任务执行之前创建可变和并发task.map列表,并且可以将结果放入该列表中……但我听说使用可变的、线程之间共享的集合根本不是最佳实践。 .. 有什么好方法可以收集所有预定的任务结果吗?我应该使用什么工具以适当的 scala 惯用方式实现这一目标,避免可变集合?

0 投票
1 回答
125 浏览

scala - F多态代码中如何使用guave缓存加载器

我有一项服务,它 从官方示例中返回笑话:

但我想用番石榴缓存缓存第一个请求的笑话(只是通过常量键,这没关系):

如您所见,cacheLoader需要“物化”值F[Joke] => Joke。并且缓存返回纯值而不F

如何在F多态代码中使用此缓存?

0 投票
1 回答
567 浏览

scala - 有没有办法在不运行的情况下将内容从 IO 提升到其他容器?

根据cats官方文档:https : //typelevel.org/cats-effect/typeclasses/liftio.html ,如果我们想从IO提升一些东西到其他容器,你应该实现LiftIO trait,但是示例显式运行unsafeRunXXX方法来获取出了效果,我想知道这是转型的唯一途径吗?

0 投票
2 回答
120 浏览

scala - 如何包装有效的 F 而不是具体的类型构造函数?

我有以下函数定义:

我想通过替换具体类型IOF[_]使其更抽象。这里的问题是:

问题是如何做到

0 投票
2 回答
366 浏览

scala - 构造有状态对象是否应该使用效果类型建模?

在使用 Scala 和 等函数式环境时cats-effect,是否应该使用效果类型对有状态对象的构造进行建模?

构造不会出错,因此我们可以使用较弱的类型类,例如Apply.

我想所有这些都是纯粹的和确定的。只是不透明,因为每次生成的实例都不同。现在是使用效果类型的好时机吗?或者这里会有不同的功能模式?

0 投票
1 回答
318 浏览

scala - 停止资源上长时间运行的进程

我有一个资源,它运行一个可能很长的阻塞操作。在实际代码中,它是一个等待下一条消息的 ZeroMQ 例程(与此处的示例代码不太相似),但在此示例中,我创建了一个虚拟循环,该循环仅在资源关闭时终止。问题是,如果我用 中断执行ctrl+c,我会得到一个异常,循环继续运行,并且close只有在我第二次点击时才被调用ctrl+c。我想如果应用程序以异常方式终止,也会发生同样的情况。

这是资源定义:

然后从main,我像这样使用它:

我希望close在执行中断时调用该方法。相反,我得到了这个:

处理这些情况的正确方法是什么?我究竟做错了什么?谢谢!

0 投票
1 回答
640 浏览

scala - 使用无标签最终更改为错误的优雅方式

我经常做这样的事情:

简而言之,这意味着 if Eitheris leftthen use raiseError,如果它right只是返回值。

有没有更方便的“解包” Either方式?

0 投票
1 回答
1371 浏览

scala - scala 猫 IOApp 应该如何获取 ExecutionContext?

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

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

但是,我正在与其他几个需要ExecutionContext. 在这种类型的应用程序中是否有推荐的获取方法?提供的好老人import scala.concurrent.ExecutionContext.Implicits.global玩得Timer[IO]好吗?

0 投票
1 回答
567 浏览

scala - How do you flatten a for-comprehension with traverse in scala cats effect?

I have some code structurally identical to this and I'm not sure the best way to clean it up? The trivial IOs and additions are there so that the example compiles without needing additional methods.

I'd really like to not have it be so nested, is there a way to have a single for comprehension that supports both IO and List? I know theres OptionT for the option variant of this problem, but there doesn't seem to be an equivalent ListT.

Any advice would be greatly appreciated

https://scalafiddle.io/sf/S79H1ZI/0

0 投票
1 回答
59 浏览

scala - Can a function operating upon mutable data structure be referentially transparent?

I'm working on an network application and designed the following trait to read files from remote machines:

The problem I see here is that the implementation accepts NetworkContext as a paramenter which is mutable and contains fields like fileDescriptor which is related to a network connection.

Is this function read referentially transparent?

I think yes, because the function itself does not provide direct access to a mutable state (it is under Sync[F].delay) even though it accepts mutable data structure as an argument.