如何使嵌套的flatMap和map更容易理解



假设我们有一个结构M:

public struct M<T> {
    let value: T
    public init(_ value: T) { self.value = value }
    public func map<U>(f: T -> U) -> M<U> { return M<U>(f(value)) }
    public func flatMap<U>(f: T -> M<U>) -> M<U> { return f(value) }
}

以及一些计算值(T)并将其作为具有M:的封装值返回的函数

func task1() -> M<Int> {
    return M(1)
}
func task2(value: Int = 2) -> M<Int> {
    return M(value)
}
func task3(value: Int = 3) -> M<Int> {
    return M(value)
}
func task4(arg1: Int, arg2: Int, arg3: Int) -> M<Int> {
    return M(arg1 + arg2 + arg2)
}

现在,假设我们想计算task1、task2和task3的值,然后将所有三个计算值作为参数传递给task4。这似乎需要使用flatMapmap:的嵌套调用

let f1 = task1()
let f2 = task2()
let f3 = task3()
f1.flatMap { arg1 in
    return f2.flatMap { arg2 in
        return f3.flatMap { arg3 in
            return task4(arg1, arg2:arg2, arg3:arg3).map { value in
                print("Result: (value)")
            }
        }
    }
}

但这看起来不太容易理解。有没有办法改善这一点?例如,使用自定义运算符?

好吧,作为参考,最好在这里记录Haskell在这种情况下所做的事情:

example1 = do
  arg1 <- task1
  arg2 <- task2
  arg3 <- task3
  value <- task4 arg1 arg2 arg3
  putStrLn ("Result: " ++ show value)

这将降级到>>=操作符,它是一个翻转的中缀flatMap:

-- (>>=) :: Monad m => m a -> (a -> m b) -> m b
-- 
-- It's a right-associative operator
example2 = task1 >>= arg1 -> 
             task2 >>= arg2 -> 
               task3 >>= arg3 -> 
                 task4 arg1 arg2 arg3 >>= value ->
                   putStrLn ("Result: " ++ show value)

所以,是的,您在这里所做的是重新发现Haskell的do表示法的动机——它正是一种用于编写嵌套flatMap的特殊平面语法!

但这里有另一个技巧,可能与这个例子有关。请注意,在您的计算中,task1task2task3没有任何相互依赖关系。这可以作为设计"平面"实用程序结构的基础,将它们合并为一个任务。在Haskell中,使用Applicative类和模式匹配可以很容易地做到这一点:

import Control.Applicative (liftA3, (<$>), (<*>))
-- `liftA3` is the generic "three-argument map" function, 
-- from `Control.Applicative`.
example3 = do
  -- `liftA3 (,,)` is a task that puts the results of its subtasks
  -- into a triple.  We then flatMap over this task and pattern match
  -- on its result. 
  (arg1, arg2, arg3) <- liftA3 (,,) task1 task2 task3
  value <- task4 arg1 arg2 arg3
  putStrLn ("Result: " ++ show value)
-- Same thing, but with `<$>` and `<*>` instead of `liftA3`
example4 = do
  (arg1, arg2, arg3) <- (,,) <$> task1 <*> task2 <*> task3
  value <- task4 arg1 arg2 arg3
  putStrLn ("Result: " ++ show value)

如果task1task2task3返回相同的类型,则另一种使其变平的方法是使用Traversable类(其底部为与上述相同的Applicative技术):

import Data.Traversable (sequenceA)
example5 = do
  -- In this use, sequenceA turns a list of tasks into a
  -- task that produces a list of the originals results.
  [arg1, arg2, arg3] <- sequenceA [task1, task2, task3]
  value <- task4 arg1 arg2 arg3
  putStrLn ("Result: " ++ show value)

因此,一个想法是构建一个提供类似功能的实用程序库。一些示例操作:

  1. 将异构类型的任务组合成一个组合。签名看起来像(M<A1>, ..., M<An>) -> M<(A1, ..., An)>
  2. 多任务映射:将n-place函数映射到生成适当类型的n任务上
  3. 将一系列任务转化为产生一系列结果的任务

请注意,#1和#2具有相等的功率。还要注意,如果我们谈论异步任务,这些操作比平面图有一个优势,那就是它们更容易并行化。