过滤一系列选项并收集所有 Somes 的值。 F# 中有内置函数吗?

Filtering a sequence of options and collecting the values of all the Somes. Is there a built-in function in F# for that?

F# 中的一个常见模式是根据是否为 ​​"Some" 来过滤某些内容,如果是,则获取其值:

module Option =
    let values s =
        s
        |> Seq.filter Option.isSome
        |> Seq.map Option.get

我想我以前在 F# 库中看到过类似的东西,但我现在找不到它们。有没有类似的功能,还是我必须用手卷的?

谢谢

最简单的方法是使用Seq.choose

s |> Seq.choose id

这里我们使用id因为输入和输出相同