Seq.map中的代码未执行?

时间:2013-12-11 05:29:09

标签: f#

我有以下代码。并且不会执行Seq.mapignore之间的代码。两个printfn未执行。为什么?

我尝试调试它并在

上设置断点
  1. links(它突出显示整个管道),
  2. printfn "Downloading"
  3. 功能printfn "Downloaded"中的
  4. download
  5. 执行将在第一个中断,但断点2,3将永远不会被击中。 (或进入)。 F#是否优化了Seq.map部分,因为忽略了结果?

    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
    

    更新 我知道for in do有效。为什么Seq.map没有?

1 个答案:

答案 0 :(得分:4)

正如约翰所说,seq是懒惰的,因为你从未实际遍历过序列(而只是通过ignore丢弃它),你传递给Seq.map的lambda中的代码是从未执行过如果您将ignore更改为Seq.iter ignore,您将遍历序列并查看所需的输出。 (任何必须遍历序列的函数都可以工作,例如Seq.count |> ignore。)

相关问题