颠倒仿函数顺序的一般方法(一个可折叠的)

general recipe for reversing order of Functors (one foldable)

假设我有类似的东西

#r @"nuget: FSharpPlus"

open FSharpPlus

let maybeXs: Option<List<int>> = Some [1]

我想将它转换成一个 List

是否有公式化机制?

let unit = Some

let almostThere: Option<List<Option<int>>> = map (fun xs -> map unit xs) maybeXs

我只需要 'eliminate' 外部选项....然后似乎特定于我的数据类型所以

let there: List<Option<int>> = 
    fold (fun s t -> t) [] (map (fun xs -> map unit xs) maybeXs)

一般来说,这是一个公平的方法吗?或者是否有一些功能性的神奇配方,可以带来一些清晰度?

只要你有一叠 Functors,而外层的 FunctorTraversable遍历 就会翻转顺序。您可以使用 sequence 简单地翻转两者:

let there = sequence maybeXs

这会产生 [Some 1]