/
formula.go
42 lines (37 loc) · 1.06 KB
/
formula.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package boolean
import (
"github.com/jamestrandung/go-accountable-calculation/acal"
)
// PerformUnaryLogicOp returns a Simple to represent the result of
// performing unaryOpFn on the values of the provided acal.TypedValue.
func PerformUnaryLogicOp[T any](
tv acal.TypedValue[T],
fnName string,
unaryOpFn func(v T) bool,
) Simple {
tv = acal.PreProcessOperand(tv)
return MakeSimpleWithFormula(
unaryOpFn(tv.GetTypedValue()),
func() *acal.SyntaxNode {
return acal.NewFormulaForFunctionCall(fnName, tv)
},
)
}
// PerformBinaryLogicOp returns a Simple to represent the result of
// performing binaryOpFn on the values of the provided acal.TypedValue.
func PerformBinaryLogicOp[T any](
tv1 acal.TypedValue[T],
tv2 acal.TypedValue[T],
op acal.Op,
opDesc string,
binaryOpFn func(a, b T) bool,
) Simple {
tv1 = acal.PreProcessOperand(tv1)
tv2 = acal.PreProcessOperand(tv2)
return MakeSimpleWithFormula(
binaryOpFn(tv1.GetTypedValue(), tv2.GetTypedValue()),
func() *acal.SyntaxNode {
return acal.NewFormulaForTwoValMiddleOp(tv1, tv2, op, opDesc)
},
)
}