gpt4 book ai didi

.net - F# 函数在没有被调用的情况下运行

转载 作者:行者123 更新时间:2023-12-04 22:01:19 24 4
gpt4 key购买 nike

这段代码

open System.Threading

let duration = 1000

module SequentialExample =
let private someTask item =
printfn "oh god why"
Thread.Sleep(duration)
item + " was computed"

let private items = [
"foo"
"bar"
"baz"
]

let getComputedItems =
printfn "heh"
[for item in items -> someTask item]
|> Array.ofList

module ParallelExample =
let private someTask item =
printfn "that's ok"
Thread.Sleep(duration)
item + " was computed"

let private items = [
"foo"
"bar"
"baz"
]

let getComputedItems =
Async.Parallel [for item in items -> async { return someTask item }]
|> Async.RunSynchronously

[<EntryPoint>]
let main args =
ParallelExample.getComputedItems |> ignore
0

具有以下输出:
heh
oh god why
oh god why
oh god why
that's ok
that's ok
that's ok

如果我调用 ParallelExample模块,为什么 F# 运行 SequentialExample 中的代码模块?

我究竟做错了什么?

最佳答案

正如约翰帕尔默在评论中所说,let getComputedItems = ...实际上是一个值,而不是一个函数,因为函数必须接受参数 .

为了使它成为一个函数,必须像这样声明它let getComputedItems () = ... .

关于.net - F# 函数在没有被调用的情况下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37381870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com