F#组合模式匹配函数

F#组合模式匹配函数,f#,composition,F#,Composition,我有以下几种: type ShouldRetry = ShouldRetry of (RetryCount * LastException -> bool * RetryDelay) and RetryCount = int and LastException = exn and RetryDelay = TimeSpan type RetryPolicy = RetryPolicy of ShouldRetry 现在我想要重试的可组合性;大概是这样的: let s

我有以下几种:

  type ShouldRetry = ShouldRetry of (RetryCount * LastException -> bool * RetryDelay)
  and RetryCount = int
  and LastException = exn
  and RetryDelay = TimeSpan

  type RetryPolicy = RetryPolicy of ShouldRetry
现在我想要重试的可组合性;大概是这样的:

let serverOverloaded = [| exnRetry<TimeoutException>;
                          exnRetry<ServerBusyException> |]
                       |> Array.map (fun fn -> fn (TimeSpan.FromSeconds(4.0)))

let badNetwork = [||] // etc

let compose p1 p2 =
  // http://fssnip.net/7h
  RetryPolicy(ShouldRetry( (fun (c,e) ->
    let RetryPolicy(ShouldRetry(fn))  = p1
    let RetryPolicy(ShouldRetry(fn')) = p2
    let (cont, delay) = fn c,e
    if cont then cont, delay 
    else
      let (cont', delay') = fn' c,e
      cont', delay') ))

let finalPolicy = serverOverloaded |> Array.scan compose (RetryPolicies.NoRetry())
让服务器重载=[| exnRetry;
exnRetry |]
|>Array.map(乐趣fn->fn(TimeSpan.FromSeconds(4.0)))
让badNetwork=[| |]//等
让我们组成p1p2=
// http://fssnip.net/7h
RetryPolicy(ShouldRetry)(fun(c,e)->
让RetryPolicy(ShouldRetry(fn))=p1
让RetryPolicy(ShouldRetry(fn'))=p2
let(cont,delay)=fn c,e
如果继续,则继续,延迟
其他的
let(cont',delay')=fn'c,e
(续,延迟)
让finalPolicy=server重载|>Array.scan组合(RetryPolicies.NoRetry())

但是我在
fn
delay
fn'
上遇到了编译器错误,说“值或构造函数'fn'未定义”。

我可以看到您的
compose
函数中存在两个问题

分解
p1
p2
时,模式需要用括号括起来(否则,编译器将代码解释为
RetryPolicy
函数的定义,而不是模式匹配):

稍晚一点调用
fn'
时,需要将参数以元组的形式传递给它(否则,编译器会认为您仅使用单个参数
c
调用
fn'
,然后构建元组):

我没有检查(或尝试运行)整个示例,因此我不知道代码的其余部分是否符合您的要求

let (RetryPolicy(ShouldRetry(fn)))  = p1 
let (RetryPolicy(ShouldRetry(fn'))) = p2 
let (cont', delay') = fn' (c,e)