Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Rob Norris
@tpolecat
If all you need is .delay you can weaken it to Sync.
Another way to do it is to have CanConstruct[F] { def make: F[Thing] } and use that everywhere, then have Sync[F] imply CanConstruct[F]. So you can get the instance at the top of your program but internal code will only know about CanConstruct.
Eugene
@eugeniyk
right, Sync will be safe since it's delay is originated from it
@tpolecat thanks, interesting trick
Applicative[F].unit.map(_ => some initialization ) why this doesn't guarantee the same effect as delay?
Rob Norris
@tpolecat
Because there is nothing about it that tells you F is capable of suspending side-effects. In general the side-effect will be immediate and the entire expression will thus become a side-effect.
It's the difference between IO(println("hi")) and List(println("hi"))
Eugene
@eugeniyk
aha, understand
Does it make sense in general to use it for lazy objects construction (that will do heavy operations like setup connection pools etc)?
Rob Norris
@tpolecat
If it's a pure value then you can use lazy val. If it's a side-effect you have to use Sync, there's no choice really.
Or (again if it's a pure value) you can use Defer[F] to put off evaluation until it's needed, or you can use Eval, which is a concrete data type specifically for talking about evaluation semantics.
But if you're interacting with the side-effecting part of the language the only thing that's guaranteed to work is Sync.
Eugene
@eugeniyk
okay, thanks!
I wonder is there a beginner cheatsheet for cats typeclasses / effects? say, for common scenario X you'd better use Y?
Ethan
@esuntag:matrix.org
[m]
There's a few images there that show the hierarchy of typeclasses, they're useful to reference when trying to find the best abstraction to use
Haemin Yoo
@yoohaemin
Hi, a quick question: why is short circuiting behavior not implemented in Apply[EitherT[F, xxx, *]].map2Eval ? We wanted to run an effect per list element -- and it kept on running after first failure when we wrote it the intuitive way by calling traverse. We then had to rewrite it using flatMap and manual recursion. Is this the desired/documented behavior?
Haemin Yoo
@yoohaemin
I see there is no law that mandates short circuiting behavior of map2Eval in the raiseError case in MonadError laws -- I think it makes sense to have it. https://github.com/typelevel/cats/blob/main/laws/src/main/scala/cats/laws/MonadErrorLaws.scala
Jasper Moeys
@Jasper-M

Is there an operation equivalent to this?

f.flatMap( foo: (A => F[G[B]]) ).map(g => Foldable[G].fold(g))

Something like f.flatFoldMap(foo) 🤔

Artem Nikiforov
@nikiforo

Is there an operation equivalent to this?

f.flatMap( foo: (A => F[G[B]]) ).map(g => Foldable[G].fold(g))

Something like f.flatFoldMap(foo) 🤔

foldMapM ?

Jasper Moeys
@Jasper-M
If I recall correctly that requires Foldable[F] instead of Foldable[G]
Sarpong Abasimi
@SarpongAbasimi
I am having a wired problem where by when I add
"org.typelevel" %% "cats-effect-testing-scalatest" % "1.1.0" in my project
everything goes
Screenshot 2021-04-28 at 20.25.55.png
and my code can't compile. When I remove it, everything goes back to normal
Oleg Pyzhcov
@oleg-py
cats-effect-testing 1.x is for cats-effect 3 (causing eviction) which has no Timer/ConcurrentEffect
Sarpong Abasimi
@SarpongAbasimi
hmm, let me double-check. thanks @oleg-py
javierg1975
@javierg1975

Hi all,
I have the following use case

def getUsers(dateWindow: Interval[LocalDate]): IO[List[User]] = ???

Ideally I'd like to iterate over the resulting Users using something like this

def getImportantUserReport(user: User): IO[Option[ImportantUserReport]] = ???

But that won't compose. As in

for {
  users<- getUsers(currentTimeWindow)
  aUser <- users  // <- NOPE
  report <- getImportantUserReport(aUser)
  // above pattern repeats multiple times
  finalResult <- someFunction
} yield finalResult

The issue here is that I need User downstream to query other services, so my current solution does this

def getImportantUserReports(user: List[User]): IO[List[(User, Option[ImportantUserReport])]] = ???

This works, but I can't help but feeling there's a more elegant solution that doesn't rely on passing data along with every function call.
Any hints will be much appreciated

Luis Miguel Mejía Suárez
@BalmungSan
traverse?
getUsers(currentTimeWindow).flatMap { users =>
  users.traverse(getImportantUserReport)
}
1 reply
Adam Rosien
@arosien
:tada:
Dylan Halperin
@dylemma

is there anything in Cats that provides an Option[Either[A, B]] => Either[A, Option[B]] functionality like

opt match {
  case None => Right(None)
  case Some(e) => e.map(Some(_))
}

?

Luis Miguel Mejía Suárez
@BalmungSan
traverse?
Well, since you already have the Option[Either[A, B]] it would be sequence
Dylan Halperin
@dylemma
hmm I swear I tried that... maybe I was missing an implicit
Rob Norris
@tpolecat
Yeah it’s sequence. If you got there via map(f).sequence you can replace that with .traverse(f)
Luis Miguel Mejía Suárez
@BalmungSan
@dylemma maybe you have an extra import? Or you do not have partial-unification on.
scala> import cats.syntax.all._
import cats.syntax.all._

scala> def test[A, B](x: Option[Either[A, B]]): Either[A, Option[B]] = x.sequence
def test[A, B](x: Option[Either[A,B]]): Either[A,Option[B]]
Dylan Halperin
@dylemma
hm yeah that works as desired, at least in this REPL session, thanks!
bblfish
@bblfish:matrix.org
[m]
Li Haoyi once wrote a small library for stateful objects that were functional. I keep seeing references to this type of structure, but I can't remember the name right now.
Rob Norris
@tpolecat
There are a few ways to do this and a few things in cats and cats-effect that help. Depends on what you really need to do.
bblfish
@bblfish:matrix.org
[m]
I was thinking of using a functional stateful object in Akka, to reduce message passing. It's a long time (5 years or so) I looked at that lib, so I am not sure it's a good idea. I just wanted to check out if it fit.
But I can't find the library I had seen on his repo anymore.
Rob Norris
@tpolecat
Not super sure what you mean by functional stateful.
bblfish
@bblfish:matrix.org
[m]
yes, I can't quite remember what it did. The argument is that you could change the object that had state, and other ojbects would get the state change on using it. But somehow it was not OO...
I think there was an example using equations and variables
God it: Rx Reactive variables. His library is here.
Christopher Davenport
@ChristopherDavenport
That is not referentially transparent.
bblfish
@bblfish:matrix.org
[m]
yes, it does not look like it is :-)
I guess what I was thinking of falls probably in the space of premature optimisation. The thought was of sharing state with child actors, since On the same VM one does not really need message passing....
bblfish
@bblfish:matrix.org
[m]
(there are blog posts on how it is an anti pattern even. :-) )
Zett98
@Zett98
one dumb question but why infix operators are rarely used in cats effect docs? why almost all of the examples use for comprehensions?
Fabio Labella
@SystemFw
I don't know of a specific reason, it's a matter of taste