参数顺序和右管道运算符

发布于 2024-10-05 19:49:29 字数 503 浏览 7 评论 0原文

有没有办法简化以下操作,这样我就不需要 runWithTimeout 函数?

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

let processOneItem item =  fetchAction item
                           |> runWithTimeout 2000

编辑: 这就是我需要额外方法的原因:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-10-12 19:49:29

也许你的意思是这样的:

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

Perhaps you mean something like this:

let processOneItem item =
  fetchAction item
  |> fun x -> Async.RunSynchronously(x, 2000)
川水往事 2024-10-12 19:49:29

我不太热衷于使用 fun x -> ... 作为管道的一部分。

我认为,当 API(例如列表)支持时,编写代码的流水线风格很好,但是当 API 不适合这种风格时,最好遵循通常的“类 C#”风格。当然,这只是个人喜好,但我只想写:

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

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
っ左 2024-10-12 19:49:29

这是一个更完整的示例,以供将来参考:

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

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

let processOneItem item =  fetchAction item
                           |> Async.Catch
                           |> fun x -> Async.RunSynchronously(x,30000)
                           |> handleExceptions
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文