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

How to stop execution in for-comprehension if Option is None using cats IO?

If i just use Option in for-comprehension everything goes as expected: val a = Some(1) val b = None val c = Some(3) val r = for { aa <- a bb <- b cc <- c } yield aa + bb + cc println(r) // None, because b is None but how to achieve the same…
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
7
votes
2 answers

Understanding IO monad

I'm trying to understand asynchronous computation by cats.effect.IO examples and got some misunderstanding. The unsafe method unsafeRunAsync seems like running the underlying effect asynchronously (I expected that some ContextShift to be provided).…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
7
votes
1 answer

What is Store in scalaz

I'm trying to understand Lenses in scalaz (surprisingly didn't find something similar in cats-core) and I came across the so called Store which is a type alias: type StoreT[F[_], A, B] = IndexedStoreT[F, A, A, B] type IndexedStore[I, A, B] =…
Some Name
  • 8,555
  • 5
  • 27
  • 77
7
votes
1 answer

Converting a list of either to a cats ValidatedNel

Given: def convert[T](list: List[Either[String, T]]): Validated[NonEmptyList[String], NonEmptyList[T]] = NonEmptyList.fromList(list) .toRight("list is empty") .flatMap(... How do I flat map the NonEmptyList[Either[String, T]] so…
Cheetah
  • 13,785
  • 31
  • 106
  • 190
7
votes
1 answer

Using functions as applicative functors/cartesians

I'm using the cats lib for this. It's easy enough to combine two lists using their applicative functor instance (or Cartesian, to be precise): import cats._ import cats.implicits._ (List(23, 4), List(55, 56)).mapN(_ + _) >> List(78, 79, 59,…
dcastro
  • 66,540
  • 21
  • 145
  • 155
7
votes
1 answer

How to use cats and State Monad

I've used cats for the first time to solve day 1 of advent of code and I'm wondering if it's possible to improve things. Given a method update with the following signature def update(i: Instruction): PosAndDir => PosAndDir I've come up with : val…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
7
votes
1 answer

How to use the free monad with Future[M[_]]

I have implemented a simple language for an ETL process using the free monad. When using List as input and output for both data fetching and storing, everything works fine. However I am using async libraries and work with Future[List] common imports…
kostja
  • 60,521
  • 48
  • 179
  • 224
7
votes
1 answer

What is the ? type?

I am trying to implement a cats Monad instance for a type that has multiple type parameters. I looked at the cats Either instance to see how it was done there. Part of the Either Monad instance code from cats is copied below: import…
mushroom
  • 6,201
  • 5
  • 36
  • 63
6
votes
1 answer

Effectful update of Ref/MVar

I'd like to apply effectual computation to the value inside MVar or Ref and atomically update it in case the computation succeeds or put back the initial value (in case of MVar)/simply do nothing(in case of Ref) in case the operation fails. I.…
Some Name
  • 8,555
  • 5
  • 27
  • 77
6
votes
4 answers

Doobie - lifting arbitrary effect into ConnectionIO

I'm trying to send an email in the same transaction as inserting user into a database with Doobie. I know that I can lift IO into ConnectionIO by using Async[ConnectionIO].liftIO(catsIO) where catsIO: IO[String] But in my code I don't operate on IO,…
Leonti
  • 10,400
  • 11
  • 43
  • 68
6
votes
1 answer

Is there any way to lift content from IO to other container without run?

According to the cats official document: https://typelevel.org/cats-effect/typeclasses/liftio.html, if we want lift something from IO to other container, you should implement LiftIO trait, but the example explicitly run unsafeRunXXX method to fetch…
6
votes
2 answers

What is the diff between implicit def and implicit val?

I have the following code, that I can not figure out, what the difference: implicit def boxPrintable[A](implicit p: Printable[A]) = p.contramap[Box[A]](_.value) implicit val stringPrintable: Printable[String] = new Printable[String]…
softshipper
  • 32,463
  • 51
  • 192
  • 400
6
votes
1 answer

value flatMap is not a member of type parameter F[Long] when using cats.effect

This perhaps been asked many times before, but none of the suggestions I've found help. I have a simple Scala code that generates long number that depends on some side-effects. I'm wrapping thing in an IO monad, but according to the least power…
edio
  • 652
  • 1
  • 8
  • 21
6
votes
2 answers

How can I validate Option values with Cats validation?

I'm trying to change code that uses cats validation, something like: case class Example(text: String, image: String) case class ValidExample(text: String, image: String) import cats.data.Validated._ import cats.implicits._ def…
ChillyPro
  • 172
  • 7
6
votes
1 answer

Cats Validated with mapN

I'm a beginner with Cats. I have an error with Validated cats. I use a list accumulator like that : case class Type( name: String, pattern: String, primitiveType: PrimitiveType = PrimitiveType.string, sample: Option[String] = None, comment:…
Robert25
  • 103
  • 6