需要返回seq <r>而不是seq <seq <r>&gt;?

时间:2015-04-26 23:20:37

标签: f#

以下函数files返回seq<seq<R>>。如何让它返回seq<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

3 个答案:

答案 0 :(得分:2)

你想将整个事情传递给Seq.collect。

例如,

files |> Seq.collect id

答案 1 :(得分:2)

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

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

  • 您可以使用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显然也会有效。

答案 2 :(得分:1)

您可以使用F#sequence expresssionseq的{​​{1}}展平为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]]

在幕后,序列表达式与> seq { for x in xss do yield! x };; val it : seq<int> = seq [1; 2; 2; 4] 做同样的事情,只是采用更加语法化的方式。

相关问题