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

Confused about cats-effect Async.memoize

I'm fairly new to cats-effect, but I think I am getting a handle on it. But I have come to a situation where I want to memoize the result of an IO, and it's not doing what I expect. The function I want to memoize transforms String => String, but…
fluffysheap
  • 769
  • 5
  • 14
4
votes
1 answer

Why is import cats.implicits._ no longer necessary for importing type class instances?

In Cats 2.1.x type class instances were brought in scope with import cats.implicits._ scala> import cats.Show import cats.Show scala> Show[Int].show(42) :13: error: could not find implicit value for parameter instance: cats.Show[Int] …
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
4
votes
1 answer

top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions

I am trying to use https://github.com/estatico/scala-newtype as follows: import io.estatico.newtype.macros.newtype import cats._ import io.databaker.env._ @newtype case class DbUrl(v: String) @newtype case class DbUser(v: String) @newtype case…
softshipper
  • 32,463
  • 51
  • 192
  • 400
4
votes
1 answer

How to use MonadError correctly?

I have the following code, that does not get compiled: final class DbSystemEnvironment[F[_] : MonadError[F, Throwable]] private(env: Environment[F]) extends DbSetting[F] { override def read(url: String, user: String, pw: String): F[DbParameter]…
softshipper
  • 32,463
  • 51
  • 192
  • 400
4
votes
2 answers

In functional Scala, what is a good way to go transform one parametrized type to another?

I need to implement a transformation from one data structure to another: A[B] => C[D] I could just implement it as a method: def transform(in: A[B]): C[D] = ??? But I would like to do it in a type-driven development way, the code being scalable,…
Vasily802
  • 1,703
  • 2
  • 18
  • 35
4
votes
1 answer

Where to use `ApplicativeError` instead of `Either`?

There is ApplicativeError[F,E] + F[A] and there is Either[E, A]. Both convey the message that the function could fail with an E or succeed with an A but I'm not sure about the different message they convey to the client about the intended way of…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
4
votes
2 answers

What is the intent behind `F[Something[F]]`?

I see a lot of F[Request[F]], F[Monitor[F]] and F[Something[F]] around. Although I understand the mechanics of how to handle this, I don't have an intuitive understanding of why some effect (F) should appear like this (both wrapping around Something…
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
4
votes
1 answer

Replace scalaz ListT with semantically equivalent cats functionality

cats does not provide ListT monad transformer so how could we rewrite the following snippet which uses scalaz ListT in a for-comprehension to a semantically equivalent snippet in cats import scalaz._ import ListT._ import scalaz.std.option._ val…
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
4
votes
2 answers

How to use Monoid Scala?

trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def mapMergeMonoid[K, V](V: Monoid[V]): Monoid[Map[K, V]] = new Monoid[Map[K, V]] { override def op(a1: Map[K, V], a2: Map[K, V]): Map[K, V] = (a1.keySet ++…
andrew17
  • 851
  • 2
  • 10
  • 25
4
votes
1 answer

How do you flatten a for-comprehension with traverse in scala cats effect?

I have some code structurally identical to this and I'm not sure the best way to clean it up? The trivial IOs and additions are there so that the example compiles without needing additional methods. I'd really like to not have it be so nested, is…
4
votes
1 answer

How to pass complete query as a parameter

I get a query from gitlab ci and I want to execute it. If I hardcode the query is the sql"""""" syntax it works. But I want to pass it as a variable(the whole query). the SqlInterperator doesn't take the value of the variable and hence returns an…
Sam
  • 497
  • 1
  • 10
  • 34
4
votes
2 answers

How to create cats IO monad from cats State

I am working with cats and I want to transform my val x: State[A, B] to StateT[IO, A, B]. Note: IO is from cats-effects. How to do this elegantly?
Mike
  • 812
  • 9
  • 25
4
votes
3 answers

How to implement if-else logic with Cats IO monad?

What is a correct way to implement if-else logic with Cats IO monad? Here is a basic example with user registration flow described with pseudo code: registerUser(username, email, password) = { if (findUser(username) == 1) "username is already in…
Alex Fruzenshtein
  • 2,846
  • 6
  • 32
  • 53
4
votes
2 answers

Using race from cats-effect prevents app from exiting

I've got simple cats-effect app, which download site from the URL given as argument. During downloading app is supposed to display "loading bar" by writting dots (.) to console. I implemented it by doing race of two IOs one for downloading another…
Stu Redman
  • 262
  • 2
  • 9
4
votes
2 answers

How to stack the State and IO monads

In a game of Connect4: we start with an empty grid two players place pieces x and o on the grid the first player to achieve 4 pieces in a line wins! this is a text based console game My thinking is that: at each step of the game the Grid is…
jakstack
  • 2,143
  • 3
  • 20
  • 37