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

Replace elements in List by condition

I have a pretty much large val s: List[Int] = //..., a function f: Int => Boolean and a function transform: Int => Int. The problem: I want to create another List[Int] such that all elements e: Int of the s: List[Int] such that f(e) = true are…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
1 answer

Free monad interpreter with side effect only

I need to write free monad interpreter which only perform effectfull actions but does not return any result. For free applicative I used interpreter with MyAction ~> Const[Unit, ?] signature and called it using foldMap function. However Const does…
schernichkin
  • 1,013
  • 7
  • 15
0
votes
1 answer

Scala Cats OptionT with Either. How to create an error value?

I am working through the excellent Scala with Cats and I'm not sure about Monad transformers. Specifically, there is an example that goes type ErrorOr[A] = Either[String, A] type ErrorOrOption[A] = OptionT[ErrorOr, A] val a = 10.pure[ErrorOrOption]…
ticofab
  • 7,551
  • 13
  • 49
  • 90
0
votes
0 answers

Is it appropriate to perfrom IO::unsafeRunSync on the same instance multiple times?

I have a question about IO monad. I opened some InputStream and want to use IO to read from it. Here is the example: def read(io: IO[Option[Array[Byte]]]): IO[Unit] = IO { io.unsafeRunSync() match { case Some(b) => //do some with b …
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
1 answer

Context bound for varargs

Few days ago I started to learn Cats and I want to implement method appendOptional for Map[String, _: Show]. I started with the following idea: def appendOptional[T: Show](to: Map[String, String], values: (String, Option[T])*): Map[String, String] =…
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
0
votes
1 answer

Different Behavior with (Cats) Functor in Scala-REPL and Compilation

import cats.{Functor, Monoid} trait Fold[I, O] { type M def m: Monoid[M] def tally: I => M def summarize: M => O } object Fold { def apply[I, O, _M](_m: Monoid[_M])(_tally: I => _M, _summarize: _M => O): Fold[I, O] = new Fold[I, O] { …
thlim
  • 2,908
  • 3
  • 34
  • 57
0
votes
1 answer

EitherT throws compilation error when returning child class

In the code below I'm using cats' EitherT class. It compiles, but I was forced to use the Error trait in: def isNeg(i:Int): Future[Either[Error,Int]] = Future { where my intent was to declare the function as: def isNeg(i:Int):…
ps0604
  • 1,227
  • 23
  • 133
  • 330
0
votes
3 answers

Scala: Append only Some's to immutable list

So say we're given a List[String] and bunch of Option[String]'s call them a, b, c. Say I want to append the valid (Some's) Options[String]'s out of a, b, c to my existingList[String]. What would be the best way to go about this using immutable…
0
votes
2 answers

How does it becomes to Id type?

I have the following code: import cats._ import cats.Functor object StudyIt { def main(args: Array[String]): Unit = { val one: Int = 1 val a = Functor[Id].map(one)(_ + 1) println(a) } } As you can see, map expects the type of…
softshipper
  • 32,463
  • 51
  • 192
  • 400
0
votes
2 answers

Sangria GraphQL Schema with EitherT result

I'm using Sangria for a Play application and currently struggling to implement the schema definition. The Problem is, that the service does not return the needed object directly, it returns an EitherT containing the object. Here are some code…
goh
  • 27
  • 5
0
votes
2 answers

Functor for sequence computation

I am reading scala cats book from underscore.io. It says following about Monad and Functor: While monads and functors are the most widely used sequencing data types.. I can see that Monad is using for sequencing data but Functor not at all.…
softshipper
  • 32,463
  • 51
  • 192
  • 400
0
votes
1 answer

Why it is possible to pass not higher kinded type?

I am reading the book https://underscore.io/books/scala-with-cats/ and trying to understand contravariant in Cats. On the page 68 there is an example of the implementation of Contravariant in Cats We can summon instances of Contravariant using the…
softshipper
  • 32,463
  • 51
  • 192
  • 400
0
votes
2 answers

How to use the decode function?

I trying to understand imap functor and have the following code: trait Codec[A] { def encode(value: A): String def decode(value: String): A def imap[B](dec: A => B, enc: B => A): Codec[B] = { val self = this new Codec[B] { …
softshipper
  • 32,463
  • 51
  • 192
  • 400
0
votes
2 answers

Upgrading Http4s to 0.18: StaticFile and fallthrough/pass

In Http4s 0.16.6a, I had the following service. import org.http4s.server.staticcontent._ import org.http4s._ object StaticFiles { val basepath = ... def apply(): HttpService = Service.lift(request => { val file = basepath +…
Toby
  • 9,523
  • 8
  • 36
  • 59
0
votes
1 answer

FS2 Running streams in sequence

I have a fairly simple use case. I have two web service calls one fetches products and another fetches relationship. I want to run fetchProducts() first extract a field from the set of products and then pass the output to fetchRelationships(ids:…