Questions tagged [scala-cats]

Cats is a library that provides abstractions for functional programming in Scala.

Cats is a library that provides abstractions for functional programming in Scala.

The name is a playful shortening of the word category.

Official Website

Official Repository

929 questions
8
votes
1 answer

Combining `OptionT` and `EitherT` to handle `Future[Either[Error, Option[T]]]`

I want to use Cats EitherT and OptionT to handle the type Future[Either[Error, Option[T]]. Suppose the following methods: def findTeacher(id: Int): Future[Either[String, Option[Teacher]]] def findSchool(teacher: Teacher): Future[Either[String,…
Amir Karimi
  • 5,401
  • 4
  • 32
  • 51
8
votes
1 answer

cats' NonEmptyList vs scala stdlib ::

I am studying the cats library recently, and I have come across this class called NonEmptyList. After reading the api, I couldn't help wondering what is it that made the cats authors to create a new class, instead of utilizing something that is…
Haemin Yoo
  • 474
  • 5
  • 11
8
votes
0 answers

Using typelevel/cats flatTraverse for different Functors

I have been trying to use more abstract functional programming concepts like those from typelevel/cats for Scala. In this specific case I am trying to eliminate the need to call map(_.flatten) after traversing some Futures. Using the standard…
steinybot
  • 5,491
  • 6
  • 37
  • 55
8
votes
1 answer

Using arbitrary trees with Free Monads in Scala+Cats

I am creating a library for grammars that will have 2 different interpretations: 1) parsing strings based on the grammar 2) generating strings in the language defined by the grammar. The library uses cats to create the AST of the grammar as a free…
Felix
  • 8,385
  • 10
  • 40
  • 59
8
votes
1 answer

Why do we need to separate Apply and Applicative type classes?

I read in cats documentation about typeclasses Apply and Applicative. I wonder why the library provides two separate type classes instead of just one type class Applicative, which would extend Functor and add ap ? Does anybody use Apply that is not…
Michael
  • 41,026
  • 70
  • 193
  • 341
8
votes
4 answers

Avoiding deeply nested Option cascades in Scala

Say I have three database access functions foo, bar, and baz that can each return Option[A] where A is some model class, and the calls depend on each other. I would like to call the functions sequentially and in each case, return an appropriate…
Ralph
  • 31,584
  • 38
  • 145
  • 282
7
votes
1 answer

Problem with given instances writing MTL style code with Scala cats

I am trying to write some Scala code to have custom behaviour in an mtl style. For example, in order to expose the "write to DB" functionality abstracting over the specific effect I wrote my own type class: trait CanPersist[M[_]]: def…
7
votes
1 answer

Using EitherT to evaluate the results of operations using a shared error type inheritance?

I have an error type hierarchy for peeling bananas: sealed trait PeelBananaError object PeelBananaError { case object TooRipe extends PeelBananaError case object NotRipeEnough extends PeelBananaError } And I have some results in EitherT that we…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
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
0 answers

Unit Testing http4s router websocket endpoints

The idea is to be able to unit test websocket endpoints on a router service. Any other kind of endpoint is fairly easy to test with a Request, but I can't figure out a way of easily testing websocket responses, as hitting the endpoint with a Request…
smetca
  • 71
  • 4
7
votes
3 answers

Convert List[Either[A, B]] to Either[List[A], List[B]]

How to convert List[Either[String, Int]] to Either[List[String], List[Int]] using a method similar to cats sequence? For example, xs.sequence in the following code import cats.implicits._ val xs: List[Either[String, Int]] = List(Left("error1"),…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
7
votes
0 answers

FS2 - How to route an element to a specific nested stream/pipe?

I want to run N nested streams/pipes in parallel and send each element to only one of the nested streams. Balance allows me to do this but I want to route elements with the same "key" to the same nested stream or pipe. I can't see any functions to…
Toby Hobson
  • 199
  • 7
7
votes
1 answer

How to create Async[Future] from Async[IO]

I am trying to implicitly add Async and Sync in my code for doobie repository. The Sync and Async[F] works fine IO. I want to convert them to Future and facing problem I have tried to create my own Aync from IO def futureAsync(implicit F:…
user11034858
  • 81
  • 1
  • 4
7
votes
1 answer

Value withFilter is not a member of Cats IO in for comprehension

I wrote this code and it compiles fine for { list : List[Int] <- Future(List(1, 2, 3)) } yield list.size res7: Future[Int] = Future(Success(3)) But if I convert this code to for { list : List[Int] <- IO(List(1, 2, 3)) } yield list.size I get…
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
7
votes
1 answer

Scala Cats State Monad

I have a class that has a one parameter method that produces a result and returns an object like itself but with updated state for subsequent use. For example, below contains a simple example of such a class and how I might use it: case class Foo(x:…
Richard Redding
  • 327
  • 2
  • 11