extend("array")
extend("string")
# Numbers
sput(
array:reduce((acc, value) => acc + value,
0,
[4,8,15,16,23,42])
) // => 108
# Strings
sput(
array:reduce((acc, value) => string:concat(acc, value),
"",
["A", "B", "C", "D", "E", "F" ,"G"])
) // => 108
# Partial usage
# 1) We define a function that adds two numbers:
add_fn = (acc, x) => acc + x
# 2) We call array:reduce with only (add_fn, initialValue) but NOT the array:
sumReducer = array:reduce(add_fn, 0)
# 3) We apply sumReducer
sput( sumReducer([1,2,3,4,5]) ) // => 15
# 4) We can reuse it for other arrays:
sput( sumReducer([10,20,30]) ) // => 60
# Another approach: `_` placeholder for the function
myPartial = array:reduce(_, 1, [2,4,6])
sput( myPartial( (acc,x)=>acc * 10 ) ) // 1000