F# 需要返回seq<;R>;而不是seq<;seq<;R>>;?

F# 需要返回seq<;R>;而不是seq<;seq<;R>>;?,f#,F#,以下函数文件返回序列。如何让它返回seq type R = { .... } let files = seqOfStrs |> Seq.choose(fun s -> match s with | Helper.ParseRegex "(\w+) xxxxx" month -> let currentMonth = ..... if currentMonth = month.[0] then doc.Lo

以下函数
文件
返回
序列
。如何让它返回
seq

type R = { .... }
let files = seqOfStrs |> Seq.choose(fun s ->
    match s with
    | Helper.ParseRegex "(\w+) xxxxx" month ->
        let currentMonth =  .....
        if currentMonth = month.[0] then
            doc.LoadHtml(s)
            Some (
                doc.DucumentNode.SelectNodes("....")
                |> Seq.map(fun tr ->
                    { ..... } ) //R. Some code return record type R. Omitted
            )
        else
            printfn "Expect %s found %s." currentMonth month.[0]
            None
    | _ ->
        printfn "No '(Month) Payment Data On Line' prompt."
        None

你想把整件事都告诉Seq.collect

比如说,

files |> Seq.collect id
您可以使用F将
seq
s的
seq
展平为
seq
。假设你有:

> let xss = seq { for i in 1 .. 2 -> seq { for j in 1 .. 2 -> i * j } };;

val xss : seq<seq<int>>

> xss;;                                                                  
val it : seq<seq<int>> = seq [seq [1; 2]; seq [2; 4]]
>让xss=seq{for i in 1..2->seq{for j in 1..2->i*j};;
val xss:seq
>xss;;
valit:seq=seq[seq[1;2];seq[2;4]]
然后你可以做:

> seq { for x in xss do yield! x };;
val it : seq<int> = seq [1; 2; 2; 4]
>seq{for x in xss do yield!x};;
valit:seq=seq[1;2;2;4]

在幕后,序列表达式与Seq.collect做了同样的事情,只是用了一种更甜的语法方式。

您的代码片段不完整,因此我们无法给您一个完全有效的答案。但是:

  • 您的代码正在使用
    Seq。选择
    ,您将返回带有值集合的
    None
    Some
    。然后你得到一系列的序列

  • 您可以使用
    Seq.collect
    将序列展平,并将
    None
    替换为空序列,将
    Some
    仅替换为序列

类似于这些思路的东西(未经测试):


其他选项,如将
Seq.concat
Seq.collect id
添加到管道末尾,显然也会起作用。

Seq.concat?完美的我试图将
Seq.choose
转换为
Seq.collect
而不删除
Some()
并将
None
更改为
Seq.emtpy
,但出现了错误。现在它起作用了。
let files = seqOfStrs |> Seq.collect (fun s ->
    match s with
    | Helper.ParseRegex "(\w+) xxxxx" month ->
        let currentMonth =  .....
        if currentMonth = month.[0] then
            doc.LoadHtml(s)
            doc.DucumentNode.SelectNodes("....")
            |> Seq.map(fun tr ->
                { ..... } ) //R. Some code return record type R. Omitted
        else
            printfn "Expect %s found %s." currentMonth month.[0]
            Seq.empty
    | _ ->
        printfn "No '(Month) Payment Data On Line' prompt."
        Seq.empty )