Welcome! Got a question? Do you have -Ypartial-unification turned on? Other FAQs: http://typelevel.org/cats/faq.html
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
.
Applicative[F].unit.map(_ => some initialization )
why this doesn't guarantee the same effect as delay?
IO(println("hi"))
and List(println("hi"))
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.
Sync
.
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?
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
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 User
s 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
getUsers(currentTimeWindow).flatMap { users =>
users.traverse(getImportantUserReport)
}
Option[Either[A, B]]
it would be sequence
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]]