【问题标题】:How can I return a Maybe value from do notation in PureScript?如何从 PureScript 中的 do 表示法返回 Maybe 值?
【发布时间】:2016-04-03 01:11:08
【问题描述】:

我试图从一个使用 do 表示法的函数返回一个 Maybe 值,但我似乎无法让它工作。这个函数接受一个字符串(“文件名”)和一个要搜索的路径...

findIn :: String -> Path -> Maybe Path
findIn search start = do
    file <- ls start
    if ((filename file) == search)
      then Just file
      else Nothing

在哪里...

ls :: Path -> Array Path
filename :: Path -> String

但我不断收到错误消息“Count not match Type Array with type Maybe”,因此编译器似乎期望 do 表示法返回一个数组。我将如何返回一个可能的值?

【问题讨论】:

    标签: maybe purescript do-notation


    【解决方案1】:

    你不能像那样混合单子。

    当你写作时:

    file <- ls start
    

    这有点像说“对于数组中的每个值 file...”所以您处于多个可能值的上下文中。

    但是剩下的代码在Maybe的上下文中,它只能处理一个(或零)值。

    在模块Data.Foldable 中有一个find 函数,它通过搜索符合某些条件的单个项目来完成主函数的大部分工作。它的实际类型更通用,但是当限制为数组时,它是这样的:

    find :: forall a. (a -> Boolean) -> Array a -> Maybe a
    

    那么你可以写:

    findIn search start = find (\x => x == search) $ ls start
    

    【讨论】:

    • 好的,我想我明白了。所以 do notation 必须返回一个数组,所有的表达式都旨在对这些数组进行过滤和映射。
    • 是的,没错。如果你想返回一个Maybe,那么do 块必须是关于Maybe。我已经更新了我的答案。
    • 感谢您澄清这一点。关于处于“可能的上下文”和“多个可能值的上下文”的注释是有道理的。我以前没有这样想过。
    【解决方案2】:

    好的,我找到了一些可行的方法,但我不确定它是否理想。

    findIn :: String -> Path -> Maybe Path
    findIn search start = if (length r == 1)
                          then head r
                          else Nothing
      where
        r = do
          file <- ls start
          guard $ (filename file) == search
          [file]
    

    所以看起来 do-notation 返回了一个 Array(Maybe Path) 类型的值。

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多