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
5
votes
1 answer

flatMap ignoring the result

I was wondering if there exists a function (in scala or cats) which omits the result within flatMap. E.g. Some("ignore this").ignoreArgumentFlatMap(Some("result")) which would be the same as Some("ignore this").flatMap(_ => Some("result"))
Nicolas Heimann
  • 2,561
  • 16
  • 27
5
votes
2 answers

Scala Cats Accumulating Errors and Successes with Ior

I am trying to use Cats datatype Ior to accumulate both errors and successes of using a service (which can return an error). def find(key: String): F[Ior[NonEmptyList[Error], A]] = { (for { b <- service.findByKey(key) } yield…
Chris Johnston
  • 335
  • 1
  • 2
  • 16
5
votes
1 answer

When exactly I have to use State Monad or .copy?

I saw an example in a book of haskell in which is created a simple stack (push, pop) and return monad states all the time they update the stack using .put(..) or .pop(). As a reminder the example looks similar to this: pop :: State Stack Int pop…
Francisco Albert
  • 1,577
  • 2
  • 17
  • 34
5
votes
1 answer

Elegant way to change either to error with tagless final

I often do things like: import cats.effect.Sync import cats.implicits._ case class User(name: String) case object Error extends Exception def validate[F[_]: Sync](name: String): F[Either[Error, User]] = Sync[F].pure(User(name).asRight) def…
5
votes
2 answers

EitherT: Call function returning Either only if a certain condition is true (otherwise return right)

So I have a certain function which I need to call only if a certain condition is true. If it's false, I consider it as Right. I would use EitherT.cond, but the thing is my function's return type is Future[Either[ErrorType, Unit]], so it's not…
5
votes
2 answers

How to write for comprehension in cats with IO monad

I have the following code: import cats.effect.IO import cats.data.State import cats.data.StateT import cats.implicits._ import cats.effect.LiftIO abstract class Example { object implicits { implicit def myEffectLiftIO:…
Mike
  • 812
  • 9
  • 25
5
votes
1 answer

Reduce / fold over list of monoid but reducer returns Either

Ive found myself in the situation a couple of times where i have a reducer / combine fn like so: def combiner(a: String, b: String): Either[String, String] = { (a + b).asRight[String] } Its a dummy implementation but the fn can fail so it…
Luke De Feo
  • 2,025
  • 3
  • 22
  • 40
5
votes
0 answers

Scala-cats: Memoization support

Scalaz provides Memo for memoization. I hope scala-cats also has a similar solution. In general I have a trait with different methods: trait t { def f1:Int = ??? def f2(i:Int):Int = ??? def f3(i:Int, s:String):String = ??? } If such solution…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
5
votes
2 answers

Implement while(true) in cats

How do you implement the following loops in cats? First (normal while(true) loop): while(true) { doSomething() } Second (while(true) loop with increment): var i = 1 while(true) { i +=1; doSomething() } Third (while(true) with several independent…
Piu130
  • 1,288
  • 3
  • 16
  • 28
5
votes
2 answers

Scala, cats, http4s - does not recognize <+> symbol from Http4s

I have created two Http4s routes: class FirstRoutes[F[_] : Async](service: FirstService[F]) extends Http4sDsl[F] { def routes: HttpRoutes[F] = HttpRoutes.of[F] { //... some code } } class SecondRoutes[F[_] : Async] extends…
Developus
  • 1,400
  • 2
  • 14
  • 50
5
votes
1 answer

Cats: mapping over tuples with the same applicative

Let's say I have: val x1: Either[String, Int] = Right(1) val x2: Either[String, Float] = Left("Nope") val x3: Either[String, Double] = Left("Not Today") I want to combine these together and get a Either[NonEmptyList[String], (Int, Float, Double)].…
Maths noob
  • 1,684
  • 20
  • 42
5
votes
1 answer

Is there a library in F# that is similar to scalaz, cats and arrow (Kotlin)?

What is a recommended approach in F# to use "high level" functional patterns? For Scala there are scalaz and cats, even there is Arrow for Kotlin, though I have not found anything similar for F#.
sampai
  • 126
  • 1
  • 4
5
votes
1 answer

Semigroup typeclass (Either) with slightly altered combine

Using cats.Semigroup one can write this: import cats.Semigroup import cats.implicits._ val l1: String Either Int = Left("error") val r1: String Either Int = Right(1) val r2: String Either Int = Right(2) l1 |+| r1 // Left("error") r1 |+| r2 //…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
5
votes
1 answer

ConcurrentHashMap with functional programming. Is suspending unsafeRun safe?

Question: Is it safe to suspend unsafeRunSync with IO? E.g. val io: IO[Unit] = //... val io2: IO[Unit] = IO(io.unsafeRunSync) The reason I would do so is that I have some class parameterized with F[_]: Effect which is like a cache: import…
Some Name
  • 8,555
  • 5
  • 27
  • 77
5
votes
2 answers

Unit-testing with cats-effect's IO monad

The Scenario In an application I am currently writing I am using cats-effect's IO monad in an IOApp. If started with a command line argument 'debug', I am delegeting my program flow into a debug loop that waits for user input and executes all kinds…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50