Before I writing my own, does anyone know if Groovy or Java has something pre-built which is similar to Excel's sumproduct function?
The quasi syntax for sumproduct is something like
def list1 = [2,3,4]
def list2 = [5,10,20]
SUMPRODUCT(list1, list2 ...) = 120
You will get 120 ((2*5) + (3*10) + (4*20) = 120)
You can transpose()
, collect()
and sum
the result:
def list1 = [2,3,4]
def list2 = [5,10,20]
assert [list1, list2]
.transpose()
.collect { it[0] * it[1] }
.sum() == 120
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With