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

How to create Kleisli with Kleisli.local (not local for transforming)

There are 2 local methods defined as: final case class Kleisli[F[_], A, B](run: A => F[B]) { self => ... def local[AA](f: AA => A): Kleisli[F, AA, B] = Kleisli(f.andThen(run)) ... } and as: sealed private[data] trait KleisliFunctions { …
Alexandr
  • 9,213
  • 12
  • 62
  • 102
0
votes
1 answer

Why are there two parameter groups in cats Bifunctor[F[_, _]].bimap function?

I stumbled upon this problem when trying to implement a Bifunctior type class for maps (Bifunctor[Map[_, _]]). Bifunctor is defined like this in cats: /** * The quintessential method of the Bifunctor trait, it applies a * function to each…
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50
0
votes
1 answer

Cats instances for Some and None et al

In Scala 2.13 and Cats, the following works fine: import cats.implicits._ Traverse[Option] However the following fails: import cats.implicits._ Traverse[Some] I would like for the latter to work, not only for Traverse of Option subclasses, but for…
eirirlar
  • 814
  • 6
  • 24
0
votes
1 answer

Scala-Cats: Validating Map's entries

Let's say I have: val m: Map[String, Int] = Map("one" -> 1, "five" -> 5, "six" -> 6, "nine" -> 9) and I have two functions: def isNotDivisibleByTwo(i: Int): ValidatedNec[String, Int] = Validated.condNec(i%2!=0, i, s"$i is divisible by 2.") def…
Maths noob
  • 1,684
  • 20
  • 42
0
votes
3 answers

cats: mapping nested either of option value

I have the following function: def function(i: Int): IO[Either[String, Option[Int]]] = ??? I want a function of the form: def foo(either: Either[String, Option[Int]]): IO[Either[String, Option[Int]]] and I want it to have the following…
Maths noob
  • 1,684
  • 20
  • 42
0
votes
1 answer

cats and function compositions

I have data types: import cats.Monoid import cats.implicits._ object Domain { case class Money(amount: Double) extends AnyVal {} implicit val moneyMonoid = new Monoid[Money] { override def combine(a: Money, b: Money): Money =…
nonlux
  • 774
  • 2
  • 6
  • 20
0
votes
1 answer

`java.lang.NoSuchMethodError: cats.FlatMap.map2` in runtime when using `.sequence`

I'm getting the following runtime error after migrating from cats v1.1.0 to v1.4.0 (An error arises from places where .sequence (cats.Traverse) is used). The code looks like: import cats.implicits._ import cats.effect.IO List(1, 2, 3).map(x =>…
oskin1
  • 13
  • 4
0
votes
0 answers

Cats - EitherT - Diverging Implicit Expansion in for comprehension w/o explicit typing?

Trying to understand a way to do this without noisy explicit typing. If I do the following: // here maybeUser is Either[ServiceError, User] // cacheService.remove(user) returns EitherT[Future, ServiceError, Unit] def blah(): EitherT[Future,…
mat4nier
  • 262
  • 4
  • 14
0
votes
1 answer

Collecting data from all tasks executed on monix Scheduler

I'm using Monix Scheduler to execute some tasks periodically. But I don't know how to not just execute them, but also to collect result from them to some collection... Lets say I have a scheduled task that returns a random number every time: val…
Lykov
  • 5
  • 1
0
votes
2 answers

What is the extra _ in this code sample from the Scala with Cats book?

I am reading the Scala with Cats book available here: https://books.underscore.io/scala-with-cats/scala-with-cats.html There is a code sample in the book that I have a question about. Basically, there is an extra underscore in the code whose purpose…
Allen Han
  • 1,163
  • 7
  • 16
0
votes
1 answer

How to convert Free monads to Free applicatives

I am using cats library and want to convert Free Monad to Free Applicative. We have a lot of code in Free monads.. But now some parts of the application has to run in parallel. There are options to use Tagless or Frees.io instead of Free.. But that…
abhishek
  • 31
  • 1
  • 6
0
votes
0 answers

How to do EitherT[Future, Failure, Iterator[A]] map with index

I use EitherT[Future, Failure, Iterator[A]]. The regular scala can map with index as below. myList.zipWithIndex.map { case (element, index) => println(element, index) s"${element}(${index})" } How to do map with index as the above for…
Tom
  • 83
  • 8
0
votes
1 answer

How to set initial value for foldLeft When Using EitherT[Future, Failure, Option[B]]

I am using EitherT[Future, Failure, Option[B]] in the following function which does foldLeft. How to set Initial value for foldLeft in the code below ? def doWork[A, B](seq: Set[A])(f: A => EitherT[Future, Failure, Option[B]]): EitherT[Future,…
Tom
  • 83
  • 8
0
votes
1 answer

Type inference failing when mapping withing a flatMap in state monad

overview I'm trying to infer types of cats.data.IndexedStateT[F[_], SA, SB, A] passed in to the flatMap method. When just using flatMap, type inference seem to infer the type parameters for SA, SB and A correctly. However, when I use map within the…
Pulasthi Bandara
  • 526
  • 4
  • 16
0
votes
3 answers

Find the top N numbers in an endless stream of integers in a functional programming style

I came across this interesting Scala problem and not sure how to solve it: class TopN { def findTopN(n: Int)(stream: Stream[Int]): List[Int] = { ??? } } This is a test of abstract data engineering skills. The function findTopN(...) in TopN…
jakstack
  • 2,143
  • 3
  • 20
  • 37