generalised sum and product with functional idiom