タイプの記録があります
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を使用します。
質問は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
考え?