cats-effect

Making sense of Scala FP Libraries

只谈情不闲聊 提交于 2020-05-09 18:47:04
问题 Just for the sake of quick clarity for someone who wants to start working with Scala FP library, on a journey to become better at pure FP. Would someone clarify the difference/relation between Cats and Cats-Effect, Cats-Effects IO? On top of that, where does Zio, and Monix stand with respect to it? Finally, what would be the relation to ScalaZ 7/8? So far based on what I have read, a good combination of the library to work with based on the documentation available and what they do would be

FS2 Stream with StateT[IO, _, _], periodically dumping state

a 夏天 提交于 2020-03-02 10:52:23
问题 I have a program which consumes an infinite stream of data. Along the way I'd like to record some metrics, which form a monoid since they're just simple sums and averages. Periodically, I want to write out these metrics somewhere, clear them, and return to accumulating them. I have essentially: object Foo { type MetricsIO[A] = StateT[IO, MetricData, A] def recordMetric(m: MetricData): MetricsIO[Unit] = { StateT.modify(_.combine(m)) } def sendMetrics: MetricsIO[Unit] = { StateT.modifyF { s =>

How to write tail-recursive functions when working inside monads

好久不见. 提交于 2019-12-03 05:58:30
问题 In general I have problems figuring out how to write tailrecursive functions when working 'inside' monads. Here is a quick example: This is from a small example application that I am writing to better understand FP in Scala. First of all the user is prompted to enter a Team consisting of 7 players. This function recursively reads the input: import cats.effect.{ExitCode, IO, IOApp} import cats.implicits._ case class Player (name: String) case class Team (players: List[Player]) /** * Reads a

Cats-effect and asynchronous IO specifics

房东的猫 提交于 2019-12-03 03:56:47
问题 For few days I have been wrapping my head around cats-effect and IO. And I feel I have some misconceptions about this effect or simply I missed its point. First of all - if IO can replace Scala's Future, how can we create an async IO task? Using IO.shift ? Using IO.async ? Is IO.delay sync or async? Can we make a generic async task with code like this Async[F].delay(...) ? Or async happens when we call IO with unsafeToAsync or unsafeToFuture ? What's the point of Async and Concurrent in cats