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

Cannot find an implicit value for ContextShift[cats.effect.IO] in http4s version 19.0.0

I have a http4s project which uses ciris for configuration management. The project is in github here. libraryDependencies ++= Seq( "is.cir" %% "ciris-cats", "is.cir" %% "ciris-cats-effect", "is.cir" %% "ciris-core", "is.cir" %%…
user51
  • 8,843
  • 21
  • 79
  • 158
6
votes
2 answers

In functional programming terms, what do you call something with an orElse or other fallback method?

Using scala for reference, we see a fallback behavior (orElse) in several places such as PartialFunction, Option, and cats EitherOps. This feels similar to but not the same as the flattening behavior of a monad. Is there a functional programming…
kag0
  • 5,624
  • 7
  • 34
  • 67
6
votes
1 answer

Cartesian product of three lists with cats

Suppose I need to zip three lists to get a list of triplets. I can write it like that: import cats._ import cats.data._ import cats.implicits._ (List(1, 2) |@| List(3, 4) |@| List(5, 6)) map {case (a, b, c) => (a, b, c)} res1: List[(Int, Int, Int)]…
Michael
  • 41,026
  • 70
  • 193
  • 341
6
votes
2 answers

Eagerly-evaluate-and-forget behavior for Cats Effect IO

I'm converting Future code to IO. I have code similar to this def doSomething: Future[Foo] = { Future { //some code the result of which we don't care about } Future { //Foo } } And then at the end of the program, I…
Lasf
  • 2,536
  • 1
  • 16
  • 35
6
votes
1 answer

Effect abstraction in Cats and parallel execution

I have the next code written using Cats IO that executes multiple actions in parallel (simplified): import cats.effect._ import cats.implicits._ import scala.concurrent.ExecutionContext.Implicits.global class ParallelExecIO { def…
Alexey Sirenko
  • 452
  • 3
  • 12
6
votes
1 answer

Why would validation break the monad laws?

On SO an explanation is given why a Validation like in scalaz, cats (Scala), or Arrow (Kotlin) can't be a monad. As far as I understand it's because they've modelled monads in terms of applicative functors and the desired behaviour of a Validation…
Jan Bols
  • 457
  • 4
  • 11
6
votes
1 answer

Cats.sequence error while applied on list of either

I want to transform a list of List[Either[String, Int]] en Either[String, List[Int]]. For this, I want to use Cats.sequence : /* for ammonite users interp.load.ivy("org.typelevel" %% "cats-core" % "1.0.1") @ */ import cats.instances.list._ import…
Moebius
  • 6,242
  • 7
  • 42
  • 54
6
votes
1 answer

Does Scala support partial application of type constructors?

I'm learning Cats from scala-exercises. Wonder how to use high order type, there are some attempt with it: trait Functor[F[_]] { def map[A, B](fa: F[A])(f: A => B): F[B] } def someThingFail1[In]() = new Functor[Function1[In, _]] { …
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
6
votes
4 answers

Making multiple API calls in a functional way

What would it be the best approach to solve this problem in the most functional (algebraic) way by using Scala and Cats (or maybe another library focused on Category Theory and/or functional programming)? Resources Provided we have the following…
6
votes
0 answers

Why does Scalaz (and Cats) use capitalised variables everywhere

For (a cut-down) example, class SemigroupOps[F] (val self: F)(val F: Semigroup[F]) extends Ops[F] { def |+|(other: => F): F = F.append(self, other) } F in this case is a variable so I'd expect to see it as f in Java-like libraries. Why does…
Toby
  • 9,523
  • 8
  • 36
  • 59
5
votes
4 answers

How to return upon encountering first "true" in a List[IO[Boolean]] in Scala Cats Effect

Say I have a set of rules that have a validation function that returns IO[Boolean] at runtime. case class Rule1() { def validate(): IO[Boolean] = IO.pure(false) } case class Rule2() { def validate(): IO[Boolean] = IO.pure(false) } case class…
iamsmkr
  • 800
  • 2
  • 10
  • 29
5
votes
1 answer

Why Semigroupal for types that have Monad instances don't combine?

I am trying wrap my head around Semigroupals in Cats. Following are statements from "Scala with Cats" by Underscore. cats.Semigroupal is a type class that allows us to combine contexts trait Semigroupal[F[_]] { def product[A, B](fa: F[A], fb:…
iamsmkr
  • 800
  • 2
  • 10
  • 29
5
votes
3 answers

Achieving Ad hoc polymorphism at function parameter level (mixing parameters of different type)

When I have a function in Scala: def toString[T: Show](xs: T*): String = paths.map(_.show).mkString And the following type class instances in scope: implicit val showA: Show[MyTypeA] implicit val showB: Show[MyTypeB] I can use function toString in…
Arjun Dhawan
  • 95
  • 1
  • 5
5
votes
1 answer

Skip errors in the infinite Stream

I have an infinite fs2.Stream which may encounter errors. I'd like to skip those errors with doing nothing (probably log) and keep streaming further elements. Example: //An example val stream = fs2.Stream .awakeEvery[IO](1.second) .evalMap(_…
Some Name
  • 8,555
  • 5
  • 27
  • 77
5
votes
1 answer

Scala: Cannot find an implicit value for ContextShift[cats.effect.IO]

I just started with scala and want to build a connection to my DB. (My knowledge stems from the scala/doobie Tutorial's on https://www.scala-exercises.org/) Now here is the Code: import doobie._ import doobie.implicits._ import cats.effect._ import…