Collapsing \ / [A, A] to A

In a web application, I have an action that can fail in various ways or ultimately succeed.

In this context, success and failure are represented by SimpleResult subclasses (representing the HTTP response)

I am using monadic operations on scalaz / to encode my algorithm with something like:

val result = for {
  user <- fetchUser \/> Forbidden("you must be connected to perform this action")
  basket <- user.basket \/> NotFound("no basket !")
  ...
} yield Ok(someBasketView(user, basket))

therefore it will be SimpleResult \/ SimpleResult, and I have to write this:

 result fold (identity, identity)

to extract the result from a disjunction, which I find pretty ugly.

Is there an abstraction that captures such a “clearly simplifiable structure”? or perhaps disjunction is not a proper abstraction in this regard?

+4
source share
1

, Scalaz merge:

scala> val e: Either[String, String] = Right("a")
e: Either[String,String] = Right(a)

scala> e.merge
res0: String = a

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> val ez: String \/ String = "a".right
ez: scalaz.\/[String,String] = \/-(a)

scala> ez.merge
res1: String = a

SimpleResult, , - , .

+7

Source: https://habr.com/ru/post/1545672/


All Articles