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

Converting functors (F[A] => G[A]) in Scala (cats or scalaz)

Is there a typeclass in Cats or Scalaz that converts between different container types? For example Option ~> Try Try ~> Future Try ~> Either Option ~> List It seems like FunctionK/~>/NaturalTransformation may be what I'm looking for, but there…
lloydmeta
  • 1,289
  • 1
  • 15
  • 25
4
votes
2 answers

Cats Seq[Xor[A,B]] => Xor[A, Seq[B]]

I have a sequence of Errors or Views (Seq[Xor[Error,View]]) I want to map this to an Xor of the first error (if any) or a Sequence of Views (Xor[Error, Seq[View]]) or possibly simply (Xor[Seq[Error],Seq[View]) How can I do this?
Ben Flowers
  • 1,434
  • 7
  • 21
  • 49
4
votes
1 answer

How to test Monad instance using discipline

Given a monad for Fun type type FUN[A] = Map[String, String] => (List[String], A) val funMonad: Monad[FUN] = new Monad[FUN] { override def flatMap[A, B](fa: FUN[A])(f: (A) => FUN[B]): FUN[B] = m => { val (list1, a1) = fa(m) val (list2,…
pawel.panasewicz
  • 1,831
  • 16
  • 27
4
votes
0 answers

Understanding the Semigroup. Functional Programming. Scala

It's easy to read and understand what the algebra of Semigroup is. But for software developer in background of imperative style languages it is still a foggy concept. Unfortunately my studies treated more about pointers than abstract algebra so now…
pawel.panasewicz
  • 1,831
  • 16
  • 27
4
votes
1 answer

Translating this monadic counter from Haskell to Scala

I have defined a monadic counter in Haskell which I'm trying to translate to Scala, failing miserably so far. The problem in a nutshell is about implementing a counter as a state monad, which reads the counter increment constant from the…
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
4
votes
0 answers

why this recursive implicit for Functor is not resolved

I am learning about Functor[F[_]]s. I would like to provide implicit Functors for all cases where I want to map over 'leaf' elements in collections. For example given instances: val collection0 = List(1,2,3) val collection1 = List(Some(1)) val…
pawel.panasewicz
  • 1,831
  • 16
  • 27
4
votes
1 answer

what is @noop in simulacrum in Cats

what is @noop annotation for cats. Basically, it doesnt accept any String alias like @op. This is scala doc for it /** * Annotation that may be applied to methods on a type that is annotated with `@typeclass`. * * Doing so results in the method…
Xiaohe Dong
  • 4,953
  • 6
  • 24
  • 53
4
votes
1 answer

Scala - JSON object is polymorphic on a field type

I'm attempting to decode some truly terrible JSON. The type information for each object is encoded within a field labeled type , i.e. "type": "event", etc. I'm using Circe for JSON encoding / decoding. The library utilizes typeclasses, where the…
penland365
  • 525
  • 4
  • 13
3
votes
1 answer

Is it possible to compose multiple interpreters for my Free Monad scala app

So i started to learn free monadic approach in my code style recently, and i hope i get the main idea of it. We have our own dsl to build a program, then we pass a compiler to a real world effect such as cats.IO and it looks good in simple programs…
3
votes
1 answer

Cats Effect 3 - Access to Blocking context

I am migrating from Cats Effect 2 to 3 and since Blocker is not available anymore I wonder how is it possible to run F in the blocking execution context? Previously, the following code was used val blocker: Blocker =…
Mike
  • 347
  • 1
  • 2
  • 15
3
votes
1 answer

How to make cats.effect.Console with timeout?

I want to take input from console in 3 seconds, otherwise in timeout case return "Timeout!". I wrote this function: def withTimeout: IO[String] = Console[IO].readLine.timeoutTo(3.seconds, IO.pure("Timeout!")) But it doesn't stop after 3 seconds,…
Max Smirnov
  • 477
  • 3
  • 10
3
votes
1 answer

How to extract value from Scala cats IO

I need to get Array[Byte] value from ioArray which is IO[Array[Byte]] // IO is from cats library object MyTransactionInputApp extends App{ val ioArray : IO[Array[Byte]] = generateKryoBinary() val i : Array[Byte] =…
user1346346
  • 195
  • 2
  • 16
3
votes
1 answer

How to combine ADTs in Scala?

I have two layers in my app: domain and application. Each layer has its own "error" ADT. For instance: package com.domain.person sealed trait DomainError case object NoPermission extends DomainError final case class Person(hasPermission: Boolean):…
kibe
  • 90
  • 1
  • 8
  • 26
3
votes
1 answer

Complex monad transformer for IO monad

I am trying to write a Cats MTL version of a function that would save an entity to a database. I want this function to read some SaveOperation[F[_]] from environment, execute it and handle possible failure. So far I came up with 2 version of this…
Leonid Bor
  • 2,064
  • 6
  • 27
  • 47
3
votes
1 answer

Scala, Quill - how to use SQL IN clause in quill query?

I have a database, which model looks like: case class Data { id: Int, value: Json } I would like to create search query which can filter some fields from value JSON by csv: def search(id: Int) = { .... val list = List("value1", "value2",…
Developus
  • 1,400
  • 2
  • 14
  • 50