小编Car*_*mbe的帖子

F#:是否有可以代替点运算符的函数?

在 F# 中,你会遇到这样的事情:

let p = Earth().GetCountry1().GetPopulation()
Run Code Online (Sandbox Code Playgroud)

有时我发现自己想要将参数与函数分开,就像在 F# 中通常所做的那样:

let p = ((Earth ()).GetCountry1 ()).GetPopulation ()
Run Code Online (Sandbox Code Playgroud)

这需要很多括号。是否有一个函数,或者有人可以想到一个函数,它可以用来代替点运算符,这有助于以更优雅的方式维护函数和参数之间的空间——像这样?

let dot method arg obj = obj.method arg
let p = Earth () |> dot GetCountry1 () |> dot GetPopulation ()
Run Code Online (Sandbox Code Playgroud)

(我的dot导致编译器错误。)

f#

1
推荐指数
1
解决办法
67
查看次数

在 FromContinuations 中使用取消延续

我正在尝试async通过Async<'T>I create with了解工作流程Async.FromContinuations,但看不到如何使用取消延续。我正在尝试这个:

open System

let asyncComputation divisor =
    Async.FromContinuations
        (fun (success, except, cancel) ->
            try
                printfn "Going to sleep..."
                Threading.Thread.Sleep 3000
                printfn "...waking up"
                1 / divisor |> ignore
                printfn "Calling success continuation..."
                success ()
            with
            | :? OperationCanceledException as e ->
                printfn "Calling cancellation continuation..."
                cancel e
            | e ->
                printfn "Calling exception continuation..."
                except e)

[<EntryPoint>]
let main argv =
    use tokenSource = new Threading.CancellationTokenSource ()
    Async.Start (asyncComputation (int argv.[0]), tokenSource.Token) …
Run Code Online (Sandbox Code Playgroud)

continuations f# asynchronous cancellation

1
推荐指数
1
解决办法
43
查看次数

为什么 Thread.Sleep 没有在 Stopwatch 中注册?

我有以下程序:

open System.Diagnostics
open System.Threading

[<EntryPoint>]
let main _ =
    let timer = Stopwatch ()
    printfn "begin"
    timer.Start ()
    Thread.Sleep 5000
    timer.Stop ()
    printfn "end"
    printfn "%i ms" timer.Elapsed.Milliseconds
    0
Run Code Online (Sandbox Code Playgroud)

我预计它会打印 5000 毫秒或更多的运行时间。它在打印“开始”和“结束”之间暂停大约 5 秒。但它只打印“3 ms”左右。为什么不Stopwatch计算睡眠时间?

.net f# sleep stopwatch

1
推荐指数
1
解决办法
120
查看次数

如何判断异步计算是否发送到线程池?

我最近被告知,在

async {
    return! async { return "hi" } }
|> Async.RunSynchronously
|> printfn "%s"
Run Code Online (Sandbox Code Playgroud)

嵌套的Async<'T>( async { return 1 }) 不会被发送到线程池进行评估,而在

async {
    use ms = new MemoryStream [| 0x68uy; 0x69uy |]
    use sr = new StreamReader (ms)
    return! sr.ReadToEndAsync () |> Async.AwaitTask }
|> Async.RunSynchronously
|> printfn "%s"
Run Code Online (Sandbox Code Playgroud)

嵌套Async<'T>( sr.ReadToEndAsync () |> Async.AwaitTask) 将是。是什么样的Async<'T>决定何时它像一个异步作业的执行是否在发送到线程池let!return!?特别是,您将如何定义发送到线程池的一个?您必须在async块中或传入的 lambda 中包含哪些代码Async.FromContinuations

f# asynchronous threadpool

0
推荐指数
1
解决办法
74
查看次数