渲染zh-Hant

summa

Translation status: 繁體中文 reader-locale proof. Code fences render through the zh-Hant pipeline; prose is canonical Latin.

Returns the sum of numeric list elements.

Syntax: <lista>.summa()

Category#

collection

Examples#

radix/corpus/lista/methodi-functionales.fab (canonical · existing-home)#

Returns the sum of numeric list elements.

# =============================================================================
# summa — Returns the sum of numeric list elements.
# =============================================================================
#
# What this teaches:
#   • higher-order intrinsics for lists — `filtrata` (filter), `mappata` (map), `reducta` (fold)
#   • closure syntax with `∴` — inline anonymous functions for collection operations
#
# Common mistakes:
#   • Omitting the initial accumulator value in reducta — reducta requires an explicit seed argument.
#
# See also: prima, ultima, lista
# =============================================================================

# lista<T> higher-order intrinsics
#
# res.filtrata(T x ∴ <pred>)                        -- filter
# res.mappata(T x ∴ <expr>)                         -- map
# res.reducta((acc, x) ∴ <expr>, init)              -- fold
#
# GRAMMAR:
#   listMethod :← expr '.' ('filtrata' | 'mappata' | 'reducta') '(' closure ')'
#
# EXPECTED OUTPUT:
#   [2, 4]
#   [2, 4, 6, 8, 10]
#   15
#
# BACKEND: Go e2e whitelist — lista intrinsic methods not yet lowered for Go
# (whitelist: lista/methodi-functionales.fab).

main {
    const _ res  [1, 2, 3, 4, 5] ∷ list<int>

    const _ pares  res.filtrata(int x ∴ x % 2  0)
    const _ duplicata  res.mappata(int x ∴ x * 2)
    const _ summa  res.reducta((int collectus, int x) ∴ collectus + x, 0)

    print pares
    print duplicata
    print summa
}

Expected output:

[2, 4]
[2, 4, 6, 8, 10]
15