标签: either

如何在Either monad的上下文中使用Data.Map.lookup?

我正在尝试使用Data.Map将字符串映射到函数.我遇到的问题是我的程序中的主要错误处理使用了Either monad,Map.lookup并将返回Maybe ([SomeVal] -> Either ValError SomeVal).我怎样才能使Map.lookup在这种情况下,无论是单子发挥很好?

apply :: String -> [SomeVal] -> Either ValError SomeVal
apply s args = (Map.lookup s prims) >>= \fn -> fn args

prims :: Map String ([SomeVal] -> Either ValError SomeVal)
prims = Map.fromList
    [("key", function)
    ,("key2", function2)
    ]

::> apply "key" [val1, val2, val3]
Run Code Online (Sandbox Code Playgroud)

haskell either maybe

1
推荐指数
2
解决办法
1108
查看次数

Scala Future的优雅处理[Either]]

我有一个形状如下的类型:

val myType: Future[Either[MyError, TypeA]] = // some value
Run Code Online (Sandbox Code Playgroud)

我知道我可以在此模式匹配并转到Right或Left类型,但问题是我必须嵌套我的模式匹配逻辑.我正在寻找更优雅的处理方式吗?有什么建议?

scala either

1
推荐指数
1
解决办法
1553
查看次数

Scala验证 - 右偏差

最后,Scala 2.12具有右偏置.我听说它可以用于验证目的,但我无法想象这一点.我无法找到好的榜样.
有人可以解释一下这个monad如何帮我验证吗?此增强的Either还可以涵盖其他用例吗?

functional-programming scala either

1
推荐指数
1
解决办法
648
查看次数

EitherT过滤器在Scalaz中出错

假设我们有EitherT[F, String, A].withFilterScalaz 的函数使用了String's Monoid 的零,以便Left在过滤器失败时填写.

因此,没有有意义的错误消息.

我怎么能实现左边的"不积极"形式的东西.

val a: Either[Future, String, Int] = -1.point[EitherT[Future, String, ?]]

val foo = for {
  aa <- a
  if aa >= 0
} yield aa
Run Code Online (Sandbox Code Playgroud)

只是hacks的方法filterwithFilter方法是否EitherT满足for-comprehension要求?

scala filter either scalaz

1
推荐指数
1
解决办法
227
查看次数

要么内在也许错误处理

是否有一个Haskell模式可以避免编写这个自定义函数?想法是将Maybe中的Nothing作为错误处理(作为包装的一部分):

eitherMaybeHandle :: String -> Either String (Maybe a) -> Either String a
eitherMaybeHandle err = \case
  Left e ->
    Left e
  Right Nothing ->
    Left err
  Right (Just a) ->
    Right a
Run Code Online (Sandbox Code Playgroud)

error-handling haskell either maybe

1
推荐指数
1
解决办法
398
查看次数

使用 Arrow-kt 和 Kotlin 处理异步结果

我有两个对外部系统的异步函数调用,返回任何一个<Exception,Something>,并且需要合并它们的结果。作为 Arrow-Kt 函数式编程的初学者,我想知道完成这项任务的最佳方式是什么。以下是我目前正在使用的代码。它当然有效,但并不真正“感觉”是最直接的。我正在寻找一种更“实用”的风格来获得结果。注意:需要预先使用成功的 List 结果。

suspend fun getAs(): Either<Exception, List<A>> = TODO()
suspend fun getBs(): Either<Exception, List<B>> = TODO()
suspend fun doSomethingWithA(listA: List<A>): Unit = TODO()

launch {
    val deferredA = async { getAs() }
    val deferredB = async { getBs() }

    either<Exception, List<A>> {
        val listOfAs = deferredA.await()
            .bimap(leftOperation = { e ->
                println("special message on error for A")
                e
            }, rightOperation = { listA ->
                doSomethingWithA(listA)
                listA
            })
            .bind()
        val listOfBs = deferredB.await().bind()

        listOfAs.filter { it.someId !in listOfBs.map …
Run Code Online (Sandbox Code Playgroud)

either async-await kotlin arrow-kt

1
推荐指数
1
解决办法
128
查看次数

编译打印时出错任何一个值

我正在尝试编译简单的代码片段.

main = (putStrLn . show) (Right 3.423)
Run Code Online (Sandbox Code Playgroud)

编译导致以下错误:

No instance for (Show a0) arising from a use of `show'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
  instance Show Double -- Defined in `GHC.Float'
  instance Show Float -- Defined in `GHC.Float'
  instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
    -- Defined in `GHC.Real'
  ...plus 42 others
In the second argument of `(.)', namely …
Run Code Online (Sandbox Code Playgroud)

haskell either

0
推荐指数
1
解决办法
77
查看次数

如何在Haskell中出错时提取Either的权利并保留其左边的信息?

我试图在值的Right构造函数中提取Either值,同时如果Either问题实际上是Left一个错误(即错误)则给出错误.Either Right Left中如何读取值的答案给了我类似的东西:

fromRight e = either (const $ error "Either Left encountered while expecting Right") id e
Run Code Online (Sandbox Code Playgroud)

这有效,但丢弃了Leftctor 的错误消息中的有用信息Either.如何发布有关相关的错误消息Left

- 编辑 -

感谢您的投入.我希望这是一个更具信息性的版本fromJust.

另外,我想避免case每次都写一个语句,并且想要避免Monads,只要它不太复杂(保持函数"eval"风格).对于我的用例,它是面向计算的,只有当提供无效输入之类的东西时才会出现错误(当没有补救措施时).

我最终使用:

fromRight e = either (error.show) id e
Run Code Online (Sandbox Code Playgroud)

haskell either

0
推荐指数
1
解决办法
356
查看次数

嵌套的Eithers具有不同的错误类型

我有一个嵌套有不同的错误类型,看起来像:

Either e1 (Either e2 a)
Run Code Online (Sandbox Code Playgroud)

我想要一个像以下一样的函数:

Either e1 (Either e2 a) -> Either e2 a
Run Code Online (Sandbox Code Playgroud)

更一般地说,是否存在匹配此模式的类型类?

haskell typeclass either

0
推荐指数
1
解决办法
202
查看次数

遍历Scala中的任何一个

我写了以下简单的代码:

import cats.effect.IO
import cats.instances.either._
import cats.syntax.TraverseSyntax

object Test extends App with TraverseSyntax{
  val e: Either[String, IO[Int]] = Right(IO(2))
  e.sequence //error here
}
Run Code Online (Sandbox Code Playgroud)

不幸的是它拒绝编译

Error:(25, 94) value sequence is not a member of scala.util.Either
Run Code Online (Sandbox Code Playgroud)

你能解释一下原因吗?我导入了either包含的实例Traverse[Either[A, ?]].怎么了?

functional-programming scala either scala-cats

0
推荐指数
1
解决办法
365
查看次数