在对一组具有类型E [String,A](其中A是多种类型)的Eithers进行匹配之后,我想将左侧的任何字符串累积到列表中.
(a, b, c, d, e) match {
case (Right(a), Right(b), Right(c), Right(d), Right(e)) => {
"All good, use a, b, c, d, and e!"
}
case anythingElse => {
val strings = accLefts(anythingElse)
doSomethingWithStrings(strings)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试.productIterator.toList使用元组,我最终会使用List [Any].如果我单独处理每个失败的案例(权利和左派的组合),我最终得到一个指数的案例陈述.
如何在那里获得一个List [[String,Any]],以传递给我的accLefts电话?或者我应该做一些比赛以外的事情?
有没有办法将Future类型的对象[未来[T1],未来[T2]]]转换为类型为[Future [T1],Future [T2]]的对象?
也许像flatMap这样适用于Either ....
我正在尝试使这个代码工作(我有类似的代码实现包裹的行动链,但它不涉及未来.它工作,更简单).下面的代码是基于此,通过必要的修改使其适用于涉及期货的情况.
case class WebServResp(msg: String)
case class WebStatus(code: Int)
type InnerActionOutType = Either[Future[Option[WebServResp]], Future[WebStatus]]
type InnerActionSig = Future[Option[WebServResp]] => Either[Future[Option[WebServResp]], Future[WebStatus]]
val chainOfActions: InnerActionSig = Seq(
{prevRespOptFut =>
println("in action 1: " + prevRespOptFut)
//dont care about prev result
Left(Future.successful(Some(WebServResp("result from 1"))))
},
{prevRespOptFut =>
println("in action 2: " + prevFutopt)
prevRespOptFut.map {prevRespOpt =>
//i know prevResp contains instance of WebServResp. so i skip the opt-matching
val prevWebServResp = prevRespOpt.get
Left(Some(prevWebServResp.msg + " & " …Run Code Online (Sandbox Code Playgroud) 我将举例说明我想立刻做些什么.
version1 :: IO ()
version1 =
if boolCheck
then case maybeCheck of
Nothing -> putStrLn "Error: simple maybe failed"
Just v -> case eitherCheck of
Left e -> putStrLn $ "Error: " ++ show e
Right w -> monadicBoolCheck v >>= \case
False -> putStrLn "Error: monadic bool check failed"
True -> print "successfully doing the thing"
else putStrLn "simple bool check failed"
Run Code Online (Sandbox Code Playgroud)
基本上我想在一些检查结果为正的情况下"做一件事".每当一次检查结果为否定时,我想保留有关违规检查的信息并中止任务.在现实生活中,这些检查有不同的类型,因此我打电话给他们
boolCheck :: Bool
maybeCheck :: Maybe a
eitherCheck :: Show a => Either a b …Run Code Online (Sandbox Code Playgroud) 略微简化,我的问题来自一个字符串列表input,我想用函数parse返回解析Either[String,Int].
然后list.map(parse)返回一个Eithers 列表.该程序的下一步是格式化错误消息,总结所有错误或传递解析的整数列表.
让我们来称呼我正在寻找的解决方案partitionEithers.
调用
partitionEithers(List(Left("foo"), Right(1), Left("bar")))
Run Code Online (Sandbox Code Playgroud)
会给
(List("foo", "bar"),List(1))
Run Code Online (Sandbox Code Playgroud)
在标准库中找到这样的东西是最好的.如果没有某种清洁,惯用和有效的解决方案,那将是最好的.还有一些高效的实用功能我可以粘贴到我的项目中就行了.
是否可以在ErrorT monad中累积错误消息?我想积累更多的错误.
我很难理解monad变换器,部分原因是大多数示例和解释都使用Haskell.
任何人都可以举一个例子来创建一个变换器来合并Javascript中的Future和Either monad以及如何使用它.
如果你可以使用ramda-fantasy这些monad 的实现,那就更好了.
我正在尝试使用Kleisli编写返回monad的函数。它适用于Option:
import cats.data.Kleisli
import cats.implicits._
object KleisliOptionEx extends App {
case class Failure(msg: String)
sealed trait Context
case class Initial(age: Int) extends Context
case class AgeCategory(cagetory: String, t: Int) extends Context
case class AgeSquared(s: String, t: Int, u: Int) extends Context
type Result[A, B] = Kleisli[Option, A, B]
val ageCategory: Result[Initial,AgeCategory] =
Kleisli {
case Initial(age) if age < 18 => {
Some(AgeCategory("Teen", age))
}
}
val ageSquared: Result[AgeCategory, AgeSquared] = Kleisli {
case AgeCategory(category, age) => Some(AgeSquared(category, age, age …Run Code Online (Sandbox Code Playgroud) 我想使用Either测试获得的结果。假设我有一个简单的示例,没有一个
@Test
fun `test arithmetic`() {
val simpleResult = 2 + 2
Assertions.assertEquals(4, simpleResult)
}
Run Code Online (Sandbox Code Playgroud)
现在我包装了结果:
@Test
fun `test arithmetic with either`() {
val result : Either<Nothing, Int> = (2 + 2).right()
Assertions.assertTrue(result.isRight())
result.map { Assertions.assertEquals(4, it) }
}
Run Code Online (Sandbox Code Playgroud)
我想它看起来有点丑陋,因为如果我们得到了最后的断言,Either.Left而不是Either.Right
如何执行,该如何断言如何以函数样式正确地测试结果?
我正在尝试Either根据选项值返回值。我的目标是如果该选项存在则返回Either.right(),否则代码应返回Either.left()。我使用 Java 8 和 vavr 0.9.2
我想避免有条件的叠瓦
public Either<String, Integer> doSomething() {
Optional<Integer> optionalInteger = Optional.of(Integer.MIN_VALUE);
Option<Integer> integerOption = Option.ofOptional(optionalInteger);
return integerOption.map(value -> {
//some other actions here
return Either.right(value);
}).orElse(() -> {
//some other checks her also
return Either.left("Error message");
});
}
Run Code Online (Sandbox Code Playgroud)
编译器失败并显示此消息
Error:(58, 7) java: no suitable method found for orElse(()->Either[...]age"))
method io.vavr.control.Option.orElse(io.vavr.control.Option<? extends io.vavr.control.Either<java.lang.Object,java.lang.Integer>>) is not applicable
(argument mismatch; io.vavr.control.Option is not a functional interface
multiple non-overriding abstract methods found in interface io.vavr.control.Option)
method io.vavr.control.Option.orElse(java.util.function.Supplier<? …Run Code Online (Sandbox Code Playgroud) 猫是否能提供类似于
implicit class FlattenListOfEither[L, R](l: List[Either[L, R]]) {
def flattenM: List[R] = l collect { case Right(v) => v }
}
Run Code Online (Sandbox Code Playgroud)
这样
val l1: List[Either[String, Int]] = List(Right(1), Right(2), Left("error"), Right(4))
l1.flattenM
Run Code Online (Sandbox Code Playgroud)
输出
List(1, 2, 4)
Run Code Online (Sandbox Code Playgroud)
类似于Vanilla Scala展平选项列表的方式
val l2: List[Option[Int]] = List(Some(1), Some(2), None, Some(4))
l2.flatten
Run Code Online (Sandbox Code Playgroud)
哪个输出
List(1, 2, 4)
Run Code Online (Sandbox Code Playgroud)
separate 提供以下语法
import cats.implicits._
val (_, rights) = l1.separate
rights
Run Code Online (Sandbox Code Playgroud)
哪个输出
List(1, 2, 4)
Run Code Online (Sandbox Code Playgroud)
但是,是否存在flatten类似开箱即用的扩展方法,该方法仅返回权限而不是元组?
either ×10
scala ×5
monads ×3
haskell ×2
scala-cats ×2
arrow-kt ×1
control-flow ×1
flatmap ×1
flatten ×1
future ×1
futuretask ×1
java ×1
javascript ×1
junit ×1
kleisli ×1
kotlin ×1
vavr ×1