我有类型的记录
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。
问题有两个:
- 有没有更简洁的方式来表达这一点?
有没有办法抽象功能/表达式
(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
想法?