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
10
votes
3 answers

Scala / Cats: How to unzip an NonEmptyList

The standard library offers the unzip method on List: scala>val l = List((1, "one"), (2, "two"), (3, "three"), (4, "four"), (5, "five")) scala> l.unzip // res13: (List[Int], List[String]) = ( // List(1, 2, 3, 4, 5), // List("one", "two",…
Valy Dia
  • 2,781
  • 2
  • 12
  • 32
10
votes
1 answer

How to separate out parsing from validation in case of versioned config using scala?

Background I have a set of configuration JSON files that look like the following: { "version" : 1.0, "startDate": 1548419535, "endDate": 1558419535, "sourceData" : [...] // nested json inside the List. "destData" : [...] // nested json…
advocateofnone
  • 2,527
  • 3
  • 17
  • 39
10
votes
1 answer

Scala Cats Effects - IO Async Shift - How Does it Work?

Here is some Scala cats code using the IO Monad: import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program…
Michael Lafayette
  • 2,972
  • 3
  • 20
  • 54
10
votes
2 answers

The object-functional impedance mismatch

In OOP it is good practice to talk to interfaces not to implementations. So, e.g., you write something like this (by Seq I mean scala.collection.immutable.Seq :)): // talk to the interface - good OOP practice doSomething[A](xs: Seq[A]) = ??? not…
lambdista
  • 1,850
  • 9
  • 16
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
9
votes
3 answers

Recovering underlying Future into Cats' EitherT's Left?

If I have a Future[Either[String, Int]] that represents either a possible error message (String) or a successful computation (Int), it is simple to move the Future's potential failure into the left side as an error message: def handleFailure(fe:…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
9
votes
1 answer

Doobie transact over a list of ConnectionIO programs

Let's say I have a list of Doobie programs (all with Unit type parameters, fwiw): val progList: List[ConnectionIO[Unit]] = prog1 :: prog2 :: ... :: Nil Is there any way I can run them in one transaction? A for-comprehension won't work here, because…
Lasf
  • 2,536
  • 1
  • 16
  • 35
9
votes
1 answer

Intersperse is missing in cats?

I only found several methods which were all unaccessible - intersperseList. Why isn't it accessible? Or is there somewhere a function which does the intersperse operation? Example of functionality from ScalaZ: scala> intersperse(List(1, 2, 3),…
monnef
  • 3,903
  • 5
  • 30
  • 50
9
votes
1 answer

How to transform Either[Future[A], Future[B]] to Future[Either[A, B]]

I have an instance of Either[Future[A], Future[B]] and I would like to transform it to Future[Either[A, B]]. Since my previous question, cats 0.8.1 has been released, changing the structure of the library and dropping Xor in favor of Either, which…
kostja
  • 60,521
  • 48
  • 179
  • 224
9
votes
1 answer

Use Unapply to extract identical type classes

I have the following situation, given two types MA and MB, I would like to be able to prove that they both not only have an Applicative but also that they both have the same underlying shape. I tried doing the following: type UnapplyM[TC[_[_]], MA,…
wheaties
  • 35,646
  • 15
  • 94
  • 131
8
votes
4 answers

List[Try[T]] to Try[List[T]] in Scala

I would like to know how to convert a List[Try[T]] into Try[List[T]] in Scala? I have tried using an accumulator and folding right but it doesn't seem ideal.
Valahu
  • 763
  • 2
  • 8
  • 15
8
votes
2 answers

Conditional state monad expressions

I'm using the State monad from the Scala Cats library to compose imperative sequences of state transitions in a functional manner. My actual use-case is quite complicated, so to simplify matters, consider the following minimal problem: there is a…
Mike Allen
  • 8,139
  • 2
  • 24
  • 46
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
8
votes
1 answer

Convert Set to cats.data.NonEmptySet?

Is there an extension method in cats for the standard Set which converts it to Option[cats.data.NonEmptySet]?
Midiparse
  • 4,701
  • 7
  • 28
  • 48
1 2
3
61 62