簡體   English   中英

F#函數運行時沒有被調用

[英]F# function running without being called

這段代碼

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模塊中運行代碼?

我究竟做錯了什么?

正如John Palmer在評論中所說, let getComputedItems = ...實際上是一個值,而不是一個函數, 因為函數必須接受一個參數

要使它成為一個函數,必須聲明它就像let getComputedItems () = ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM