Reference · Vectors

vec.cumsum

signature
vec.cumsum(list | null) -> list

What it does

The running total: a list where each item is the sum of the input up to and including that position. The last item is the sum of the whole list. Ints stay ints, floats stay floats, as with +.

Examples

print(vec.cumsum([1, 2, 3, 4]))print(vec.cumsum([0.5, 0.25, 0.25]))print(vec.cumsum([1, -3, 2]))
output
[1, 3, 6, 10][0.5, 0.75, 1.0][1, -2, 0]

Edge cases

print(vec.cumsum([]), vec.cumsum(null), vec.cumsum([7]))print(vec.cumsum([0.1, 0.2, 0.3]))print(vec.cumsum([1, 0.5, 2]))
output
[] [] [7][0.1, 0.30000000000000004, 0.6000000000000001][1, 1.5, 3.5]

Errors

An item that is not an int or a float, or an argument that is not a list, is a runtime error, not an error value. A total past 64 bits is an integer overflow runtime error.

this one fails
print(vec.cumsum([1, "a"]))
output
line 1: `vec.cumsum` expects numbers, got int and str
this one fails
print(vec.cumsum([9223372036854775807, 1]))
output
line 1: integer overflow

Chaining

A list of daily sales piped through vec.cumsum is the running total to each day:

let sales = [3, 5, 4, 6, 8]print(sales | vec.cumsum)print(sales | vec.cumsum | last)
output
[3, 8, 12, 18, 26]26

See also

vec.cumprod, vec.diff, sum, vec.rolling_sum

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On vec.cumsum(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo