scala-cats

Optimizing a Free Monad

牧云@^-^@ 提交于 2021-02-19 03:44:10
问题 If I have a value a: Free[Op, A] , is it possible to "flatten" the structure of a so that two Op s that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program. 回答1: As far as I understand there is no way for this kind introspection of Free

Scala: SeqT monad transformer?

廉价感情. 提交于 2021-02-08 04:48:09
问题 If we have such two functions... def findUserById(id: Long): Future[Option[User]] = ??? def findAddressByUser(user: User): Future[Option[Address]] = ??? ...then we are able to use cats OptionT monad transformer to write for-comprehension with them easily: for { user <- OptionT(findUserById(id)) address <- OptionT(findAddressByUser(user)) } ... I'd like to compose future of sequences this way, like this: def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ??? def

Scala: SeqT monad transformer?

佐手、 提交于 2021-02-08 04:47:34
问题 If we have such two functions... def findUserById(id: Long): Future[Option[User]] = ??? def findAddressByUser(user: User): Future[Option[Address]] = ??? ...then we are able to use cats OptionT monad transformer to write for-comprehension with them easily: for { user <- OptionT(findUserById(id)) address <- OptionT(findAddressByUser(user)) } ... I'd like to compose future of sequences this way, like this: def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ??? def

Use mapN to apply values

我的未来我决定 提交于 2021-01-29 22:32:29
问题 I have the following code snippet: final case class Configuration(env: Env, user: String, password: String, address: String) trait DbSetup[F[_]] { type EnvT[A] = OptionT[F, A] def system: EnvT[Env] def user: EnvT[String] def password: EnvT[String] def address: EnvT[String] } object DbSetup { def get[F[_] : Monad](s: DbSetup[F]): s.EnvT[Configuration] = ??? } How to use Applicative function mapN in the implementation of get function to get Configuration filled? 回答1: Try import cats.syntax

EitherT with multiple return types

我的未来我决定 提交于 2021-01-28 02:05:19
问题 I am trying to compose futures with for-comprehension and EitherT, but I am having trouble due the return types. Please can someone explain why this does not compile and how can I make it compile changing the for-comprehension? import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import cats.data.EitherT import cats.implicits._ object CatsApp extends App { case class L1(a: String) case class L2(a: String) case class L3(a: String) case class R1(num: Int)

Option[io.databaker.env.EnvValue], but type F is invariant in type

半城伤御伤魂 提交于 2021-01-27 11:53:45
问题 I have the following code snippet, that does not get compiled: trait Environment[F[_]] { def get(v: EnvVariable): F[Option[EnvValue]] } final class LiveBadEnvironment[F[_] : Sync] extends Environment[F] { override def get(v: env.EnvVariable): F[Option[env.EnvValue]] = None.pure[F] } the compiler complains: [error] found : F[None.type] [error] required: F[Option[io.databaker.env.EnvValue]] [error] (which expands to) F[Option[io.databaker.env.EnvValue.Type]] [error] Note: None.type <: Option[io

Recovering underlying Future into Cats' EitherT's Left?

家住魔仙堡 提交于 2020-12-29 12:33:48
问题 If I have a Future[Either[String, Int]] that represents either a possible error message ( String ) or a successful computation ( Int ), it is simple to move the Future 's potential failure into the left side as an error message: def handleFailure(fe: Future[Either[String,Int]]) = f.recover({ case e: Exception => Left(s"failed because ${e.getMessage}")) I would expect something similar to exist for EitherT , but maybe I just can't find out what it is called. It is relatively simple, but

Recovering underlying Future into Cats' EitherT's Left?

独自空忆成欢 提交于 2020-12-29 12:33:46
问题 If I have a Future[Either[String, Int]] that represents either a possible error message ( String ) or a successful computation ( Int ), it is simple to move the Future 's potential failure into the left side as an error message: def handleFailure(fe: Future[Either[String,Int]]) = f.recover({ case e: Exception => Left(s"failed because ${e.getMessage}")) I would expect something similar to exist for EitherT , but maybe I just can't find out what it is called. It is relatively simple, but