Questions tagged [cats-effect]

Part of the Scala Cats ecosystem for an IO type and associated effects.

191 questions
27
votes
1 answer

Making sense of Scala FP Libraries

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,…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
26
votes
1 answer

Cats-effect and asynchronous IO specifics

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…
ukulele
  • 317
  • 4
  • 9
18
votes
1 answer

How to write tail-recursive functions when working inside monads

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…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
13
votes
2 answers

How to make virtual time pass in a test in Cats Effect 3?

I am trying to convert Mules to Cats Effect 3 (CE3). Since it is a caching library, in its tests it needs (virtual) time to pass to test whether items will be expired. It is currently making fairly heavy use of cats.effect.laws.util.TestContext,…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
13
votes
1 answer

Cats effect - parallel composition of independent effects

I want to combine multiple IO values that should run independently in parallel. val io1: IO[Int] = ??? val io2: IO[Int] = ??? As I see it, I have to options: Use cats-effect's fibers with a fork-join pattern val parallelSum1: IO[Int] = for { …
amitayh
  • 770
  • 1
  • 8
  • 19
12
votes
1 answer

scala cats ambiguous implicit values

import cats._ import cats.implicits._ trait Console[F[_]]{ def readInput() : F[Int] def print(msg: String) : F[Unit] } class Foo { def doFoo[F[_]: Monad](number: Int)(implicit C: Console[F]) : F[Unit] = { C.readInput().flatMap{input =>…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
12
votes
2 answers

cats-effect:How to transform Map[x,IO[y]] to IO[Map[x,y]]

I have a map of string to IO like this Map[String, IO[String]], I want to transform it into IO[Map[String, String]]. How to do it?
Kumar Waghmode
  • 509
  • 2
  • 18
9
votes
2 answers

Doobie - lifting arbitrary effect into ConnectionIO CE3

I am trying to migrate project from cats-effect 2 to cats-effect 3, i am using doobie for interacting with database. Previously i could lift ConnectionIO to IO as it was described, but with the upgrade i didn't find any implementation of…
poweright
  • 93
  • 5
9
votes
2 answers

Should constructing stateful objects be modeled with an effect type?

When using a functional environment like Scala and cats-effect, should the construction of stateful objects be modeled with an effect type? // not a value/case class class Service(s: name) def withoutEffect(name: String): Service = new…
Mark Canlas
  • 9,385
  • 5
  • 41
  • 63
8
votes
1 answer

Expected behavior in cats-effect for exceptions thrown in `.delay` or `.map`

During a PR review, I was asked to replace Sync[F].delay with Sync[F].catchNonFatal because an exception might be thrown. This does work: scala> Sync[IO].delay(throw new Exception).recover{ case t: Throwable => 42 }.unsafeRunSync res10: Int =…
betehess
  • 839
  • 4
  • 19
8
votes
1 answer

How to add proper error handling to cats-effect's Resource

I am trying to get some basic file IO (write/read) in a purely functional way using cats-effect. After following this tutorial, here is what I ended up with for reading a file: private def readFile(): IO[String] = for { lines <- …
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
7
votes
1 answer

Is it okay to use "unsafeRunSync()" in Cats-Effects?

I am using Doobie and in the examples that I found, it uses unsafeRunSync, like: sql"select name from country" .query[String] // Query0[String] .to[List] // ConnectionIO[List[String]] .transact(xa) // IO[List[String]] …
7
votes
1 answer

Why is Deferred factory method has return value in the context of F

I'm looking at cats.effect.concurrent.Deferred and noticed that all pure factory methods inside its companion object return the F[Deferred[F, A]], not just Deferred[F, A] like def apply[F[_], A](implicit F: Concurrent[F]): F[Deferred[F, A]] = …
6
votes
0 answers

What is the current cats-effects IO Error handling state of arts?

I have been browsing a lot around the subject. Just from stackoverflow, the best comprehensive post i found were Try[Result], IO[Result], Either[Error,Result], which should I use in the end How to add proper error handling to cats-effect's…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
6
votes
1 answer

Improving performance of fs2 stream involving file transformation

I've got something like this (it's an example from https://github.com/typelevel/fs2, with my additions, which I marked with comments): import cats.effect.{Blocker, ExitCode, IO, IOApp, Resource} import fs2.{io, text, Stream} import…
1
2 3
12 13