我想了解我應該如何撰寫回傳“復雜”型別(例如F[Either[A,B]])的 Kleislis。這是我的意思的一個例子:
這是一個簡單的案例 - 給定兩個 Kleislis
val parseRegistration: Kleisli[Result, Auth.Registration, Auth.Registration] = ???
val hashPassword: Kleisli[Result, Auth.Registration, Auth.Registration] = ???
我可以這樣組合它們:
val validateAndHash = parseRegistration andThen hashPassword
一切都很好。
但是,考慮到更復雜的 Kleisli 型別(回傳值為 an Either[A, B]),我無法直接組合它們(因為我最終得到F[Either[A,B]]了 Kleisli 期望的地方F[A])。
// relevant type definitions
final case class ServiceError(kind: ErrorKind, message: String = "", cause: Option[Throwable] = None)
final type Result[A] = Either[ServiceError, A]
final case class Registration(email: String, password: String)
final case class Registered(session: Session)
val createUser: Kleisli[F, Auth.Registration, Result[Long]] = ???
val createSession: Kleisli[F, Long, Result[Session]] = ???
// does not compile (no overloaded alternatives match the arguments) - I expected this as
// createUser returns F[Result[Long]] and the createSession Kleisli expects only F[Long]
val createUserAndSession = createUser andThen createSession
好的,正如預期的那樣。然后,我沒有撰寫它們,而是嘗試簡單地執行嵌套映射:
val r1 = validateAndHash(info) // Result[Auth.Registration]
val r2 = r1.map(i => createUser(i)) // Either[ServiceError, F[Result[Long]]]
val r3 = r2.map(_.map(_.map(uid => createSession(uid).map(_.map(Auth.Registered.apply)))))
// Now I've got an Either[ServiceError, F[Either[ServiceError, F[Either[ServiceError, Auth.Registered]]]]]
// in r3! I wanted to end up with F[Result[Auth.Registered]]
所以我最終導致嵌套增加,并且flatten似乎沒有幫助,因為F并且Result不能相互扁平化。
我確信這是經驗豐富的 scala 貓程式員一直遇到的問題,所以如果您能幫助我了解如何避免讓自己陷入這種混亂,我將不勝感激?
編輯:
我現在定義了一個函式來反轉嵌套的包裝器型別,以便它們可以被展平:
def invert[S, F[_]: Monad, V](e: Either[S, F[V]]): F[Either[S, V]] =
e match {
case Left(s) => Monad[F].pure(Left(s))
case Right(fv) => fv.map(Right(_))
}
這允許我這樣做:
val r1 = validateAndHash(info)
val r2 = invert(r1.map(i => createUser(i))).map(_.flatten)
val r3 = r2.map( i => invert(i.map(uid => createSession(uid).map(_.map(Auth.Registered.apply)))).map(_.flatten))
val r4 = Monad[F].flatten(r3)
哪個看起來……笨拙?而且太具體了Either。有沒有一種更清潔的方法可以使用貓內置的東西來做到這一點?
uj5u.com熱心網友回復:
我寧愿撰寫值也不愿玩弄Kleisli,尤其是因為EitherT這很簡單。
createUser首先,讓我們將&重構createSession為普通方法:
def createUser[F[_]](registration: Registration): F[Result[Long]] = ???
def createSession[F[_]](long: Long): F[Result[Session]] = ???
現在讓我們實施 createUserAndSession
import cats.Monad
import cats.data.EitherT
def createUserAndSession[F[_]](registration: Registration)(implicit ev: Monad[F]): F[Result[Session]] =
EitherT(createUser(registration)).flatMapF(createSession).value
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/412166.html
標籤:
上一篇:加特林的自定義饋線
