下面的函数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发布于 2015-04-27 00:01:40
你的片段是不完整的,所以我们不能给你一个完全有效的答案。但是:
Seq.choose,您正在返回None或Some以及值集合。然后你得到一个序列..。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.concat或Seq.collect id添加到管道的末尾,显然也是有效的。
发布于 2015-04-26 23:32:16
你想把整件事都输给Seq.collect。
例如,
files |> Seq.collect id发布于 2015-04-26 23:57:37
可以使用F# 序列高速公路将seq of seqs扁平化为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相同的事情,只是以一种更语法更甜的方式。
https://stackoverflow.com/questions/29884792
复制相似问题