Order of arguments and pipe-right operator

☆樱花仙子☆ 提交于 2019-12-11 18:24:35

问题


Is there a way to simplify the following, so I won't need a runWithTimeout function?

let runWithTimeout timeout computation =
   Async.RunSynchronously(computation, timeout)

let processOneItem item =  fetchAction item
                           |> runWithTimeout 2000

Edit: Here's why i needed the extra method:

let processOneItem item =  fetchAction item
                           |> Async.Catch
                           |> runWithTimeout 2000
                           |> handleExceptions

回答1:


Perhaps you mean something like this:

let processOneItem item =
  fetchAction item
  |> fun x -> Async.RunSynchronously(x, 2000)



回答2:


I'm not very keen on using fun x -> ... as part of a pipeline.

I think that the pipelining style of writing code is nice when it is supported by the API (e.g. lists), but when the API doesn't fit the style, it is just better to follow the usual "C#-like" style. Of coures, this is just a personal preference, but I'd just write:

let processOneItem item =  
  let work = Async.Catch(fetchAction item)
  let result = Async.RunSynchronously(work, 30000)
  handleExceptions result



回答3:


Here's a more complete sample, for future reference:

let processOneItem item =  fetchAction item
                           |> Async.Catch
                           |> fun x -> Async.RunSynchronously(x,30000)
                           |> handleExceptions


来源:https://stackoverflow.com/questions/4308363/order-of-arguments-and-pipe-right-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!