968 questions
0
votes
1
answer
95
views
AWS Lambda not works with Cats Effect IO (Scala)
I try to run function written on Scala with Cats IO using. Simple Scala functions from https://aws.amazon.com/blogs/compute/writing-aws-lambda-functions-in-scala/ and https://rockthejvm.com/articles/...
1
vote
1
answer
109
views
Traversable functor with product of two applicatives in Scala
I have been trying to traverse a data structure in Scala and do two things at the same time. I have been using Cats library for this. Here's a little example which does not compile. I do not ...
0
votes
1
answer
60
views
TypeLevel Scala compilation errors for HTTP Service when using http4sVersion dependency
Am trying to write a sample HTTP Service using TypeLevel Scala which hits the National Weather Service API. Am having type mismatch errors from the http4sVersion dependency and
EntityEncoder from cats....
2
votes
1
answer
59
views
FunctionK transformation between two ReaderT instances providing the environment to the result
I have the following definition of effect types that I use in my service:
type Traced[F[_], A] = ReaderT[F, TracingCtx, A]
type TracedErrorHandling[F[_], E, A] = Traced[EitherT[F, ...
0
votes
1
answer
72
views
How to change monoid for option of endofunctor?
I want to combine two endofunctors in the context of Option. The combining I want is by composing two endofunctors into one via Category.compose. I found that MonoidK[Endo].algebra[*] instance for ...
1
vote
0
answers
94
views
FS2 who to do a onCancelWith
I have a grpc service which ofer an API looking like that def f(input: fs2.Stream[F, Input]) : fs2.Stream[F, Output]
the Input is basically formed like
sealed trait Input
case Data(x : ByteBuffer) ...
1
vote
1
answer
379
views
Scala Fs2: Aggregate computation on infinite streams
I cant seem to understand how to perform aggregate computations on infinite streams. Taking an infinite stream of elements and performing a computation on each one individually is easy, but collecting ...
2
votes
0
answers
126
views
Are BFS and DFS the same and just the Monad is different?
I wrote a tail-recursive BFS in Scala:
import scala.collection.mutable
/** Do BFS from start and return the smallest distance to end (None if not connected) */
def bfs[A](start: A, end: A, neighbors: ...
1
vote
1
answer
122
views
How can I update a variable within my Http4s service every set interval of time?
I am trying to continually refresh a value after a set interval of time within an Http4s service. Up until now it's been defined as just a val that maintains its initial value. I would like to ...
0
votes
1
answer
126
views
Set up and use the Cats library for typeclass derivation in my Scala 3.4.2 project
Good Day,
I am using Intellij 2024.1.3 and
Oracle OpenJDK 22.0.1
this is my build.sbt:
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "3.4.2"
lazy val root = (...
1
vote
0
answers
57
views
How to combine multiple ordered streams efficiently?
Given a List[Stream[F[_], A], where A <: Ordered, and each stream contains an ordered list of elements, what is an effective way to combine these streams into a single, ordered stream of all ...
0
votes
1
answer
156
views
EitherT with Scala type alias
Try to use type alias with EitherT get compilation error
type FuEiErr[T] = Future[Either[Error, T]]
type FuEiErrInt = Future[Either[Error, Int]]
case class Error(msg: String)
def fA(x:Int): FuEiErr[...
1
vote
0
answers
72
views
I use Scala 3.3.3 and would like to test a small program using import cats.kernel.laws.IsEq - I get this error: laws is not a member of cats.kernel
I am using Scala 3.3.3 and would like to test a small program using import cats.kernel.laws.IsEq, I get this error message: "laws" is not a member of cats.kernel and here is my
build.sbt:
...
1
vote
1
answer
80
views
Incorrect Show auto derivation involving NonEmptyList
This program:
package domain
import cats.{Show, derived}
import cats.data.NonEmptyList
import cats.implicits._
case class Error(code: String, message: String)
case class Errors(errors: NonEmptyList[...
2
votes
1
answer
240
views
ScalaFX and Cats Effect
I am trying to integrate Cats Effect into a ScalaFX desktop application, and I am having trouble getting the tasks to execute. I would like to run a background thread/fiber to initialize the window ...
0
votes
2
answers
105
views
Where Is The Implementation for Traverse[IO]
val foo: IO[List[Int]] = List(IO.pure(100)).sequence
Where do I find the implementation for the sequence method? I presume there is a Traverse typeclass implementation for cats.effect.IO and where ...
0
votes
0
answers
142
views
Changing error type of ApplicativeError/MonadError
I use a library that produces a result with some custom error type. This library defines MonadError for that result type. For compatibility with the rest of my application, I'm trying to get ...
0
votes
1
answer
113
views
Why does Maven download hundreds of versions of maven-metadata.xml for dependency when I explicitly stated a non-snapshot version in pom.xml?
At my company we're upgrading to a new version of Scala but when I run mvn clean install, near the end of the build, it spends 22 minutes (!) trying to download metadata about these 3rd party ...
1
vote
1
answer
155
views
Handling Exceptions in Scala FS2 Stream Transformation flow
import cats.effect.{IO, IOApp}
import fs2.Pipe
import fs2.Stream
object Test extends IOApp.Simple {
final case class Student(id: Int, name: String)
private val studentData: Map[Int, Student] = ...
0
votes
1
answer
92
views
How to call Cats typeclass method without specifying it explicitly?
I want to write that code:
IO.foreverM:
IO.sleep(1.seconds) *> IO.println("Tick")
But it doesn't compile. However this code compiles:
FlatMap[IO].foreverM:
IO.sleep(1.seconds) *> ...
1
vote
1
answer
56
views
What does `=>` mean in scala generics?
Reading both the documentation for Scalaz and Cats, I notice that they frequently use => in their generic/polymorphic field. For example, from the scalaz page on applicatives, it has the following ...
2
votes
0
answers
79
views
Resource.fromAutoCloseable not invoking the inside action
In my code I need to create a database and create some tables in it using cats.effect. The code as following,
def createTable[F[_]: Async]: Resource[F, ResultSet] = {
val createTableQuery = &...
2
votes
1
answer
192
views
Get current contiuation in Scala
Haskell has a function for getting the current continuation
getCC = callCC (\c -> let x = c x in return x)
How to write a similar function in Scala?
E.g. function callCC presents in cats.ContT. ...
3
votes
1
answer
106
views
is it a bad practice to map a suspended effect with an impure function?
Assuming getData() returns an IO[_] and has all its side effects suspended, I fail to see any difference between
getData().map(d => println(d.toString))
and
getData().flatMap(d => IO(println(d....
1
vote
1
answer
112
views
How to make cancellable timeout callback?
I want that user can run a timer with callback and able to cancel it. Something like this:
def main: F[Unit] =
for
cancel <- runTimer(callback, 5.seconds)
shouldCancel <- askUser
...
0
votes
0
answers
49
views
Why we can't have the two issues about variance arisen in book Scala with Cats achieved both. Give me an example for each issue
Scala with Cats arise two issues as follow:
And give the answer:
But it seems that it could, like:
sealed trait A
final case object B extends A
final case object C extends A
trait Printer[-T]:
...
2
votes
0
answers
72
views
Scala 3 given/implicit resolution doesn't work as expected
I'm working on the Chapter 11 Case Study: CRDTs from the excellent book "Scala with Cats". The code in the book is written using Scala 2, but I've modified it for Scala 3, specifically 3.3.1....
0
votes
1
answer
242
views
Is there a way to transform a Tuple of Resources into a Resource of Tuple?
I'm trying to port Scala2 / shapeless code to Scala3, and one thing I miss is a way to convert a HList of Resource to a single, type-safe Resource.
In Scala2, I am able to use cats.sequence.Traverser'...
0
votes
2
answers
126
views
Create FunctionK instance with anonymous function
I want to create a FunctionK instance with anonymous function. Here is the minimal example of that:
import cats.~>
given (Option ~> List) = {
case Some(a) => a :: Nil
case None => ...
1
vote
1
answer
241
views
Using an Applicative Functor Functions with Cats and Scala
How can I implement a similar Haskell function in Scala 3 and Cats?
ghci> (+) <$> (+1) <*> (+1) $ 10
22
There was a solution using mapN. It is mentioned here, Using functions as ...
0
votes
0
answers
109
views
scala cats, deal with race condition
i'm lookning for solution to eleminate race condition in my code.
Basically i have a write and read functions. These functions are called in a stream, of values.
Stream itself is going through ...
0
votes
1
answer
84
views
How to synchronize a bunch of effects in only one thread at a time
i have a small problem.
for
map <- kafkaEventHoldLine.get // Ref[F, List[String]]
key = dr.derived + dr.metricId.toString
_ <- if !map.contains(key) then
...
0
votes
2
answers
149
views
Scala cats exception handling
Why does it not catch and print the exception msg from calculateTwo? If we make calculateOne throw the exception, the exception is caught and the msg is printed.
package com.oxo.test
import ...
0
votes
1
answer
78
views
Scala EitherT Future Exception handling
In the below method, only the exception coming from burnService.execute is getting caught in the catch. Any exception coming from earnService.execute is not getting caught. If I move the earnService,...
0
votes
2
answers
274
views
parEvalMap with buffering
I'm interested in a version of parEvalMap with a bounded buffer so that it will keep progressing the underlying stream as long as its buffer is not full.
What I mean is onsidering a stream of values [...
0
votes
1
answer
80
views
Scala, Slick, Cats - How to map different SQL errors with OptionT?
I have a simple slick query which run on database:
def method(): Future[Either[Error, MyCustomDTO]] =
OptionT(database.run(query))
.map(MyCustomDTO(_))
.toRight(dataNotFound())
.value
The ...
0
votes
1
answer
51
views
How to combine results with fold without calling unsafe method inside
Let's say that I have the following case classes:
case class Record(a: String, b: String)
case class Result(c: Int, d: Int)
And a function that does a bunch of things and then returns an IO[Result]
...
-1
votes
2
answers
108
views
How can I make interval action with flag in scala cats
I need to run IO action on interval if flag is set to true. Flag could be set to true multiple times during one interval from other places of program. For example:
class IntervalAction {
def touch: ...
0
votes
1
answer
184
views
Ambiguous Implicit Resolution Error With Doobie
I have this function that inserts a One To Many relationship representation between 3 tables:
def insertRelations(cs: TableA): Fragment = {
sql"""INSERT INTO my_schema.table_a (
...
0
votes
1
answer
195
views
Scala Cats Effect Wrapping an Implementation from the Effect Type of the Enclosing Class
I have an implementation that looks like this:
final class MyServiceImpl[M[_]: Async](dbCfg: DBConfig)(implicit ec: Scheduler) extends MyService[M] {
......
......
......
......
}
Inside this ...
0
votes
1
answer
306
views
Making Implemetation More Generic With Scala and Cats
I have this in-memory implementation of a simple Cache in Scala using cats effects.
Here is my trait:
trait Cache[F[_], K, V] {
def get(key: K): F[Option[V]]
def put(key: K, value: V): F[Cache[F, ...
0
votes
1
answer
592
views
How to avoid a race condition when using cats-effect based workers?
I created a tiny worker system to run parallel jobs with maximum multi-core processor utilization.
It seems to work fine, but at some point, when working with a larger amount of jobs, an error appears ...
2
votes
1
answer
650
views
How to shutdown Cats Effect Runtime
Is there a way to shutdown the cats effect runtime from within an application, similar to System.exit(0)?
There is a shutdown method on IORuntime but it is not documented and it behaves weirdly when I ...
2
votes
2
answers
257
views
EitherT not properly upcasting to common trait super type (Scala 3)
When chaining a for comprehension with EitherT, the compiler is having issues upcasting my errors to their common ancestor type.
I have the following type definitions in my code:
sealed trait Error
...
3
votes
1
answer
470
views
http4s-ember-server throws ClosedChannelException from time to time
Using http4s-ember-server on one of my applications I notice from time to time these exceptions appearing in my server's logs.
What is odd about them is that they don't follow my logger's appender or ...
0
votes
1
answer
161
views
Doobie upgrade from 1.0.0-RC2 to 1.0.0-RC4 causes fragments.or not to compile
My code (below) is failing to compile after a Doobie library upgrade. fragments.or(filterFragments: _*) "Cannot resolve overloaded method 'or'". Presumably the signature has changed but I ...
0
votes
1
answer
135
views
Timeout for Type class Async
I am relatively new to cats effect. We are currently using Cats effect 2.x.
I would like to understand how can we apply timeout for Async Type class.
def myMethod[F[_]: Async, A]: EitherT[F, Error, A] ...
1
vote
1
answer
222
views
join on cats-effect fiber doesn't raise inner error
A cats-effect fiber, once started, a reference kept of it, some other code executed and then rejoined, won't raise the errors that happen inside.
Do you know why .join doesn't throw an error and why ...
0
votes
1
answer
200
views
Scala - Retry on a for comp of futures (SoftwareMill Retry)
I have some code that attempts to execute a series of futures within a for comp and returns the value of the completion of the comp. I want to implement a retry function on the entirety of the for ...
0
votes
1
answer
86
views
Filter operation with a typeclass return type in Scala
I need to filter a list of items with a function that performs external call and returns a boolean wrapped into something (let's say a Future). So basically:
def filter[A](list: List[A])(f: A => ...