From 279ca3ac92a8a0252d94d88067f5c2e659502b97 Mon Sep 17 00:00:00 2001 From: Tommy van Leeuwen Date: Thu, 1 Apr 2021 05:03:50 +0200 Subject: [PATCH] Use varargs for And/Or rule. --- rule.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/rule.go b/rule.go index c82f755..37634b1 100644 --- a/rule.go +++ b/rule.go @@ -8,31 +8,39 @@ type Rule interface { } // And returns a new rule whereby BOTH of the passed-in rules must be satisfied for the rule to be satisfied -func And(r1, r2 Rule) Rule { - return andRule{r1, r2} +func And(r ...Rule) Rule { + return andRule{r} } // Or returns a new rule whereby ONE OF the passed-in rules must be satisfied for the rule to be satisfied -func Or(r1, r2 Rule) Rule { - return orRule{r1, r2} +func Or(r ...Rule) Rule { + return orRule{r} } type andRule struct { - r1 Rule - r2 Rule + r []Rule } func (ar andRule) IsSatisfied(index int, record *TradingRecord) bool { - return ar.r1.IsSatisfied(index, record) && ar.r2.IsSatisfied(index, record) + for _, r := range ar.r { + if !r.IsSatisfied(index, record) { + return false + } + } + return true } type orRule struct { - r1 Rule - r2 Rule + r []Rule } func (or orRule) IsSatisfied(index int, record *TradingRecord) bool { - return or.r1.IsSatisfied(index, record) || or.r2.IsSatisfied(index, record) + for _, r := range or.r { + if r.IsSatisfied(index, record) { + return true + } + } + return false } // OverIndicatorRule is a rule where the First Indicator must be greater than the Second Indicator to be Satisfied