4

我有类型的记录

type tradeLeg = {
    id : int ;
    tradeId : int ;
    legActivity : LegActivityType ;
    actedOn : DateTime ;
    estimates : legComponents ;
    entryType : ShareOrDollarBased ;
    confirmedPrice: DollarsPerShare option;
    actuals : legComponents option ; 


type trade = {
    id : int ;
    securityId : int ;
    ricCode : string ;
    tradeActivity : TradeType ;
    enteredOn : DateTime ;
    closedOn : DateTime ;
    tradeLegs : tradeLeg list  ;
}

显然,tradeLegs 是一种交易类型。一条腿可能已结算或未结算(或未结算但价格已确认) - 因此我定义了活动模式:

let (|LegIsSettled|LegIsConfirmed|LegIsUnsettled|) (l: tradeLeg) = 
        if Helper.exists l.actuals then LegIsSettled
        elif Helper.exists l.confirmedPrice then LegIsConfirmed
        else LegIsUnsettled

然后确定交易是否已结算(基于所有匹配 LegIsSettled 模式的边:

let (|TradeIsSettled|TradeIsUnsettled|) (t: trade) = 
        if List.exists (
            fun l -> 
                match l with 
                    | LegIsSettled -> false 
                    | _ -> true) t.tradeLegs then TradeIsSettled
        else TradeIsUnsettled

我可以看到这种使用活动模式的一些优点,但是我认为有一种更有效的方法可以查看列表中的任何项目是否匹配(或不匹配)活动模式,而无需专门为它,并使用 List.exist。

问题有两个:

  1. 有没有更简洁的方式来表达这一点?
  2. 有没有办法抽象功能/表达式

    (fun l -> 
          match l with 
          | LegIsSettled -> false 
          | _ -> true)
    

这样

let itemMatchesPattern pattern item  =
    match item with
         | pattern -> true
         | _ -> false

这样我可以写(因为我正在重用这个设计模式):

let curriedItemMatchesPattern = itemMatchesPattern LegIsSettled
if List.exists curriedItemMatchesPattern t.tradeLegs then TradeIsSettled
        else TradeIsUnsettled

想法?

4

2 回答 2

7

为了回答你关于活动模式的问题,让我用一个更简单的例子:

let (|Odd|Even|) n = 
  if n % 2 = 0 then Even else Odd

当您使用 声明具有多个选项的模式时,(|Odd|Even|)编译器会将其理解为返回类型值的函数Choice<unit, unit>。因此,您可以使用的活动模式是整个组合|Odd|Even|,而不仅仅是您可以独立使用的两个构造(例如|Odd||Even|)。

可以将活动模式视为一等函数,但如果您使用具有多个选项的模式,则无法使用它:

让模式 = (|奇|偶|);; val 模式:int -> 选择

您可以编写函数来测试一个值是否与指定的模式匹配,但是您需要很多函数(因为有许多Choice类型被类型参数的数量重载):

let is1Of2 pattern item = 
  match pattern item with
  | Choice1Of2 _ -> true
  | _ -> false

> is1Of2 (|Odd|Even|) 1  
val it : true

像这样的东西在你的情况下会起作用,但它远非完美。

如果您声明多个部分活动模式,您可以做得更好(但是您当然会失去完整活动模式的一些好的方面,例如完整性检查):

let (|Odd|_|) n = 
  if n % 2 = 0 then None else Some()  
let (|Even|_|) n = 
  if n % 2 = 0 then Some() else None

现在你可以编写一个函数来检查一个值是否匹配模式:

let matches pattern value = 
  match pattern value with
  | Some _ -> true
  | None -> false

> matches (|Odd|_|) 1;;
val it : bool = true
> matches (|Even|_|) 2;;
val it : bool = true

总结虽然可能有一些或多或少优雅的方式来实现您所需要的,但我可能会考虑活动模式是否比使用标准函数给您带来任何更大的优势。最好先使用函数来实现代码,然后决定哪些构造可用作活动模式,然后再添加活动模式。在这种情况下,通常的代码看起来不会差很多:

type LegResult = LegIsSettled | LegIsConfirmed | LegIsUnsettled

let getLegStatus (l: tradeLeg) =    
    if Helper.exists l.actuals then LegIsSettled   
    elif Helper.exists l.confirmedPrice then LegIsConfirmed   
    else LegIsUnsettled

// Later in the code you would use pattern matching
match getLegStatus trade with
| LegIsSettled -> // ...
| LegIsUnSettled -> // ...

// But you can still use higher-order functions too
trades |> List.exist (fun t -> getLegStatus t = LegIsSettled)

// Which can be rewritten (if you like point-free style):
trades |> List.exist (getLegStatus >> ((=) LegIsSettled))

// Or you can write helper function (which is more readable):
let legStatusIs check trade = getLegStatus trade = check
trades |> List.exist (legStatusIs LegIsSettled)
于 2010-04-19T23:36:20.090 回答
4

除了 Tomas 关于活动模式的实际细节的观点外,请注意,您始终可以缩短fun x -> match x with |...function | ...,这将节省一些击键次数以及弥补潜在无意义标识符的需要。

于 2010-04-19T23:41:56.043 回答