Array.sum
func Array.sum[T: Number](xs: Array[T]): T | nullArray.sum returns the sum of the elements of xs, or null when xs is empty. Write Array.sum(xs) ?? 0 for a zero default, or ?? 0.0 for floats. It adds from the left with +, so it panics when a partial sum overflows.
Details
- Returns
nullwhenxsis empty, not a zero, so give it a default with??. - Integer overflow panics, as it does for
+. ANaN, or infinities of both signs, giveNaN.
Basic examples
Array.sum([1, 2, 3, 4]) // 10
Array.sum([2.5, 0.25]) // 2.75An empty array gives null. Give a default with ??:
let empty: Array[int] = []
Array.sum(empty) // null
Array.sum(empty) ?? 0 // 0Call it through . on the array:
let prices = [4.5, 12.0, 3.25]
prices.sum() // 19.75Scope
A single element is its own sum:
[42].sum() // 42Every numeric type works, and the sum keeps the element type:
[byte(200), byte(50)].sum() // 250
[int8(-100), int8(-28)].sum() // -128
[float32(0.5), float32(0.25)].sum() // 0.75
[int64(1), int64(2), int64(3)].sum() // 6Negative numbers cancel:
[5, -3, -2].sum() // 0Infinities and NaN follow IEEE 754:
[1.0, Math.Infinity].sum() // Infinity
[Math.Infinity, -Math.Infinity].sum() // NaN
[1.0, Math.NaN, 2.0].sum() // NaNThe values of a map are an array, so Array.sum takes them:
let stock = Map{ apples: 3, pears: 5, plums: 0 }
stock.values().sum() // 8In a pipe, the array is the first argument:
[1, 2, 3] | Array.map({ x * x }) | Array.sum() // 14Applications
Total a field of a list of records by mapping to the field first:
type Item = { name: string, price: float64, qty: int }
let cart = [
Item{ name: "pen", price: 1.5, qty: 4 },
Item{ name: "pad", price: 3.0, qty: 2 },
]
cart.map({ x.price * float64(x.qty) }).sum() ?? 0.0 // 12.0Average a list of scores, with null when there are none:
func average(xs: Array[float64]): float64 | null {
let total = xs.sum()
if total == null {
return null
}
total / float64(len(xs))
}
println(average([90.0, 75.0, 84.0]))
println(average([]))83.0
nullCount the characters of a list of words:
let words = ["ant", "colony", "queen"]
words.map({ x.length() }).sum() // 14Add up the numbers in a line of text, skipping what does not parse:
let line = "12 7 x 30"
let ns: Array[int] = []
for part in line.split(" ") {
let n = part.toInt()
if n != null {
ns.push(n)
}
}
ns.sum() ?? 0 // 49Properties and relations
On a non-empty array of integers, Array.sum agrees with a fold through + that starts at zero:
let xs = [3, -7, 4, 1]
assert(xs.sum() == xs.reduce(0, func(a: int, b: int): int { a + b }))The integers from 1 to n add up to n * (n + 1) / 2:
for n in [1, 10, 100] {
assert(Array.range(1, n + 1).sum() == n * (n + 1) / 2)
}n copies of one integer add up to n times it:
assert(Array.filled(5, 7).sum() == 35)When no partial sum overflows, integer sums do not depend on the order of the elements, and splitting an array splits its sum:
let xs = [5, 2, 9, 2]
let ys = [1, 8]
assert(xs.sum() == xs.reverse().sum())
assert(xs.concat(ys).sum() == (xs.sum() ?? 0) + (ys.sum() ?? 0))Possible issues
The result is T | null, not T, so using it as a T is a compile error:
let xs = [1, 2, 3]
println(xs.sum() + 1) // error: + needs both operands of one type, got int | null and intGive a default with ??:
let xs = [1, 2, 3]
(xs.sum() ?? 0) + 1 // 7The default must have the element type. Numeric types never mix, so ?? 0.0 on an Array[int] does not give a float64:
let xs = [1, 2, 3]
let total: float64 = xs.sum() ?? 0.0 // error: cannot use int where float64 is expectedInteger overflow panics. The sum of small types overflows sooner:
[byte(200), byte(100)].sum() // panic: Array.sum: integer overflow
[9223372036854775807, 1].sum() // panic: Array.sum: integer overflowConvert the elements to a wider type first, or wrap on purpose with Bits.wrapAdd:
let xs = [byte(200), byte(100)]
xs.map({ int(x) }).sum() // 300
xs.reduce(byte(0), Bits.wrapAdd) // 44An array whose element type is a union, such as int | null or Number, has no single T, so it is a compile error:
let xs: Array[int | null] = [1, null, 3]
println(xs.sum())
// error: T would be int | null, which is not a single member of its bound Number; narrow firstCollect the non-null elements into an Array[int] first. Testing each one narrows it:
let xs: Array[int | null] = [1, null, 3]
let ns: Array[int] = []
for v in xs {
if v != null {
ns.push(v)
}
}
ns.sum() // 4Because T never includes null, a null result always means an empty array.
Named primitive types, strings, and records are not Number:
type Cents = int
let fees = [Cents(150), Cents(275)]
println(fees.sum())
// error: T would be Cents, which is not a member of its bound Number; unwrap it with int(...)Unwrap a named primitive with its base type, and wrap the total again:
type Cents = int
let fees = [Cents(150), Cents(275)]
Cents(fees.map({ int(x) }).sum() ?? 0) // Cents(425)To join strings, use String.join:
String.join(["a", "b", "c"], "") // "abc"One NaN makes the result NaN. Drop it first with Math.isNaN:
let xs = [1.0, Math.NaN, 3.0]
xs.filter({ !Math.isNaN(x) }).sum() // 4.0See also
Array.reduce · Array.max · Array.min · Array.map · Array.length · Bits.wrapAdd · Math.isNaN · String.join · arithmetic operators · ?? operator
