需要 return seq<R> 而不是 seq<seq<R>>?

Need to return seq<R> instead of seq<seq<R>>?

下面的函数filesreturnsseq<seq<R>>。怎么改成returnseq<R>

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# sequence expresssionseqseq 扁平化为 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]]

那么你可以这样做:

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

在幕后,序列表达式做的事情与 Seq.collect 相同,只是语法更甜。

您的代码片段不完整,因此我们无法为您提供完整的答案。但是:

  • 您的代码正在使用 Seq.choose,您将返回 NoneSome 以及值集合。然后你得到一个sequences序列...

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

类似的东西(未经测试):

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 )

在管道末尾添加 Seq.concatSeq.collect id 等其他选项显然也可以。