我有以下代码。并且不执行Seq.map和之间的代码ignore。两人printfn没有被处决。为什么?
我试图调试它并设置断点
links (它突出了整个管道), printfn "Downloading" printfn "Downloaded"在功能上download。执行将在第一个处中断,但永远不会命中断点 2、3。(或被踏入)。Seq.map由于结果被忽略,F# 是否优化了部件?
let download url =
printfn "Downloaded"
() // Will complete the function later.
let getLinks url =
....
|> Seq.toList
let .....
async {
......
links = getLinks url // Tried to modify getLinks to return a list instead of seq
........
links // execution hit this pipe (as a whole)
|> Seq.map (fun l ->
printfn "Downloading" // execution never hit here, tried links as Seq or List
download l)
|> ignore
Run Code Online (Sandbox Code Playgroud)
更新:
我知道for in do有效。为什么Seq.map不呢?
正如约翰所说,seq是懒惰的,并且因为您从未真正遍历序列(而只是通过 丢弃它ignore),您传递给的 lambda 中的代码Seq.map永远不会执行。如果您更改ignore为,Seq.iter ignore您将遍历序列并查看您想要的输出。(任何必须遍历序列的函数都可以工作,例如Seq.count |> ignore。)