简体   繁体   中英

F# function running without being called

This code

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

Has the following output:

heh
oh god why
oh god why
oh god why
that's ok
that's ok
that's ok

If I'm calling ParallelExample module, why is F# running the code in SequentialExample module?

What am I doing wrong?

As John Palmer said in comments, let getComputedItems = ... is actually a value, not a function, because a function has to take an argument .

To make it a function, one have to declare it like let getComputedItems () = ... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM