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

Scala: no-name parameters in function with List and Option

2 different examples, the first one works: import cats.syntax.either._ val e = 10.asRight[String] def i2s(i:Int):String = i.toString e.map(i => List(i2s(i))) //using explicit parameter e.map(List(i2s(_))) //using no-name _ parameter Now…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
4
votes
1 answer

How to work with timer in cats effect without ExecutionContext.global and IOApp?

I have a simple sequence of IO operaion with 5 second pause. implicit val timer = IO.timer(ExecutionContext.global) def doSth(str: String): IO[Unit] = IO(println(str)) def greeting(): IO[Unit] = doSth("Before timer.") *> …
Vadim
  • 753
  • 8
  • 22
4
votes
2 answers

Tail recursive algorithm for generating all topological orderings in a graph

Given a graph i need to generate all topological orderings. For instance, given the following graph: i want to generate all topological orderings, which are: 2 4 7 5 2 7 4 5 2 4 5 7 Because many topological orderings may exist, I need to generate…
Kevin
  • 2,813
  • 3
  • 20
  • 30
4
votes
1 answer

MonoidK and Monad relation

I'm trying to understand well known phrase A monad is just a monoid in the category of endofunctors and map some category theory concepts to cats library. There is a MonoidK typeclass in cats and it's polymorphic on combineK method. So one instance…
Bogdan Vakulenko
  • 3,380
  • 1
  • 10
  • 25
4
votes
1 answer

Composition of Readers for Dependency Injection in Scala

Here is a simple service example, whose methods return reader: trait Service1_1{ def s1f1:Reader[Map[String,Int],Int] = Reader(_("name")) def s1f2:Reader[Map[String,Int],Int] = Reader(_("age")) } Here is a service-consumer, that…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
4
votes
1 answer

How to pass implicit vals defined through package objects from Scala in Java

I am using the Cats library. In Scala the code looks like: import cats.Semigroupal import cats.instances.option._ val r = Semigroupal.tuple2(Option(1), Option(2)) The tuple2 defined as: def tuple2[F[_], A0, A1](f0:F[A0], f1:F[A1])(implicit…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
4
votes
1 answer

Model multiple function calls with a stream (in a safe, FP way)

Given a function A => IO[B] (aka Kleisli[IO, A, B]) that is meant to be called multiple times, and has side effects, like updating a DB, how to delegate such multiple calls of it into a stream (I guess Pipe[IO, A, B]) (fs2, monix…
V-Lamp
  • 1,630
  • 10
  • 18
4
votes
1 answer

Getting around invariant result type in State

I would like to define a State that builds a concrete subtype of a trait, as per decodeFoo: sealed trait Foo case class Bar(s: String) extends Foo case class Baz(i: Int) extends Foo val int: State[Seq[Byte], Int] = State[Seq[Byte], Int] { case bs…
Synesso
  • 37,610
  • 35
  • 136
  • 207
4
votes
1 answer

Chaining a number of transitions with the state Monad

I am starting to use the state monad to clean up my code. I have got it working for my problem where I process a transaction called CDR and modify the state accordingly. It is working perfectly fine for individual transactions, using this function…
Luis Sisamon
  • 101
  • 7
4
votes
1 answer

Cats Effect IO: Compose IO with Scala collections

This is a toy Scala program, that reads 10 numbers from the console and adds 1 to each prints them out: import cats._ import cats.effect._ import cats.implicits._ object Main extends IOApp { def printLine[A](x: A)(implicit show: Show[A]):…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
4
votes
1 answer

How to go from a List[F[String]] to F[List[String]] using Scala and Cats?

I'm new to Cats and I don't know how to overcome this situation. In the code bellow: class Example[F[_]] { import cats._ import cats.data._ import cats.syntax.all._ def saveAll(list: List[String])(implicit M: Monad[F]): F[List[String]] = { …
user10503105
4
votes
1 answer

Recursions with the State-Monad

I am using the State-Monad from the cats library to take care of the state of a card game I am implementing in Scala. I have a function simulateGame which should end as soon as the status of a the current state is Over. The function looks like…
4
votes
1 answer

How do I explain the difference between fold and foldK?

In the context of a programmer newly learning functional programming and completing the online Scala Exercises for Cats here, the following result seems puzzling: import cats._ import cats.implicits._ object Foo { def main(args: Array[String]):…
David Rawson
  • 20,912
  • 7
  • 88
  • 124
4
votes
1 answer

Twitter Future & Cats Arrow

I'm trying to combine Twitter Future with Cats Kleisli and Arrow and I have a compilation error that I don't know how to solve it. The code is the following: package com.example import scala.language.higherKinds import cats.arrow.Arrow import…
Octavian R.
  • 1,231
  • 8
  • 12
4
votes
1 answer

Replacing all JSON String values recursively with Circe

Using the CIRCE library & Cats, it would be incredibly useful to be able to transform all the string values of an arbitrary Json object such as { "topLevelStr" : "topLevelVal", "topLevelInt" : 123, "nested" : { "nestedStr" : "nestedVal" }, …
Tobias Roland
  • 1,182
  • 1
  • 13
  • 35