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
2 answers

How to turn `Either[Error, Option[Either[Error, Account]]]` to `Either[Error, Option[Account]]` with typelevel cats?

I'm using cats, wonder how to turn a data with it. From val data = Either[Error, Option[Either[Error, Account]]] to val target: Either[Error, Option[Account]] = howToConvert(data) If there is any Error happens, the result will be Left(error) with…
Freewind
  • 193,756
  • 157
  • 432
  • 708
5
votes
1 answer

How to convert a `NonEmptyList[Either[Error, User]]` to `Either[Error, NonEmptyList[User]]` with cats?

I'm using cats, wonder how to turn a data with it: val data = NonEmptyList[Either[Error, User]] to val target: Either[Error, NonEmptyList[User]] = howToConvert(data)
Freewind
  • 193,756
  • 157
  • 432
  • 708
5
votes
2 answers

Scala Cats or Scalaz typeclass scanLeft like

I am wondering if there is a typeclass in Cats or Scalaz which offers an operator like this: def scan[G[_],A,B](zero: B)(g: G[A],f: (A,B) => B):G[B] Or if there exists some mathematical definition for such operator (something like Monad for…
salc2
  • 577
  • 5
  • 14
5
votes
3 answers

List of Kleisli to Kleisli of list

I was wondering if there is a way to turn List[Kleisli[Option, Int, Int]] to Kleisli[Option, Int, List[Int]]. In particular I have the list of kleisli formed like this: def k(a: String) = Kleisli[Option, Int, Int](m => Some(a.length * m)) val kList…
gurghet
  • 7,591
  • 4
  • 36
  • 63
5
votes
1 answer

Cats-effect and the IO monad

I've been trying to grasp IO monad for a while now and it makes sense. If I'm not mistaken, the goal is to split the description of the side effect and the actual execution. As in the example below, Scala has a way to get an environment variable…
Mauro Palsgraaf
  • 237
  • 3
  • 13
5
votes
2 answers

Scala Free Monads with Coproduct and monad transformer

I'm trying to start using free monads in my project and I'm struggling to make it elegant. Let's say I have two contexts (in reality I have more) - Receipt and User - both have operations on a database and I would like to keep their interpreters…
Leonti
  • 10,400
  • 11
  • 43
  • 68
5
votes
0 answers

Why is Try the default implicit Applicative instance in cats

Playing around with cats, I have noticed a certain behavior which I cannot quite explain: import cats.implicits._ ... def wrapA[A, F[_]](v: A)(implicit F: Applicative[F]): F[A] = F.pure(v) not a particularly useful method, just playing around.…
kostja
  • 60,521
  • 48
  • 179
  • 224
5
votes
1 answer

How to fold a collection of endomorphism with cats

Given a function def f(i: I) : S => S I would like to write a pretty common combinator g def g(is : Seq[I], init: S) : S The easy implementation uses only classic scala def g(is : Seq[I], init: S) : S = is.foldLeft(init){ case (acc, i) =>…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
5
votes
1 answer

Difference between Semigroup and SemigroupK

In cats there are 2 semigroup types classes: Semigroup and SemigroupK with the latter working on type constructors. I fail to see the advantages of the latter over the former. If I look at the list instances they are providing Monoid (although there…
Bruno Bieth
  • 2,317
  • 20
  • 31
5
votes
1 answer

Fold on NonEmptyList

I'm trying out cats library but I have hard time navigating between things I should import and create. My problem is as follows: sealed trait Checks case class CheckViolation(id: Long, msg: String) extends Checks case class ChecksPassed(ids:…
almendar
  • 1,823
  • 13
  • 23
4
votes
2 answers

Why is FunctionK not the same as a Natural Transformation

The cats documentation on FunctionK contains: Thus natural transformation can be implemented in terms of FunctionK. This is why a parametric polymorphic function FunctionK[F, G] is sometimes referred as a natural transformation. However, they are…
Joe
  • 1,479
  • 13
  • 22
4
votes
1 answer

How monoids generalize over types

I was playing with cats' Monoids in scala when I see that the monoid operations are extended for Tuples in a natural way: import cats.Monoid object mon { implicit object IntMonoid extends Monoid[Int] { def combine(a: Int, b: Int) = a*a + b*b …
Dioni
  • 118
  • 7
4
votes
0 answers

Where should I put typeclass instances?

Lets say I develop a new public data structure, lets call it FancyList. It has no external dependencies and everybody loves it. This FancyList turns out to be a Functor, Monad, MonoidK, and Traverse. What is the best way to provide cats instances…
cmhteixeira
  • 877
  • 11
  • 22
4
votes
2 answers

What does suffix -al convey in Semigroupal in contrast to Semigroup?

There exist Semigroup and Semigrupal Semigroup[Int].combine(1, 41) // : Int = 42 Semigroupal[Option].product(Some(1), Some(41)) // : Option[(Int, Int)] = Some(value = (1, 41)) What is -al suffix meant to convey? In what sense does…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
4
votes
0 answers

Combining Routes in Http4S

Browsing the doc i see: Multiple HttpRoutes can be combined with the combineK method (or its alias <+>) tweetService <+> helloWorldService I wonder what it is the outcome of that ? i.e. Combing to Kleisli with CombineK…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127