Handbook
Glossary
sum ( seq -- n )
Factor handbook
»
The language
»
Collections
»
Sequence operations
»
Sequence combinators
Prev:
produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
Next:
product ( seq -- n )
Vocabulary
sequences
Inputs
seq
a
sequence
of
number
s
Outputs
n
a
number
Word description
Outputs the sum of all elements of
seq
. Outputs zero given an empty sequence.
Examples
USING: prettyprint sequences ; { 3 1 5 } sum .
9
USING: prettyprint sequences ; { } sum .
0
Definition
IN:
sequences
GENERIC:
sum
( seq -- n )
Methods
USING:
math
sequences
sequences.cords
;
M:
cord
sum
[
sum
]
cord-both
+
;
inline
USING:
kernel
math
sequences
;
M:
iota
sum
length
dup
1
-
*
2/
;
inline
USING:
kernel
math
sequences
;
M:
object
sum
0
[
+
]
binary-reduce
;
inline
USING:
kernel
math
ranges
sequences
sequences.private
;
M:
range
sum
dup
length
[
drop
0
]
[
swap
[
first-unsafe
]
[
last-unsafe
]
bi
+
*
2
/
]
if-zero
;
USING:
accessors
kernel
math
sequences
;
M:
repetition
sum
[
elt>>
]
[
length>>
]
bi
*
;
inline
USING:
generic
kernel
math.vectors.simd
math.vectors.simd.intrinsics
math.vectors.simd.private
sequences
;
M:
simd-128
sum
dup
simd-rep
[
(simd-sum)
]
[
M\
simd-128
sum
(call-next-method)
]
v->x-op
;
inline
USING:
accessors
kernel
math
sequences
tensors
tensors.private
;
M:
tensor
sum
vec>>
0
<simd-slice>
[
simd-slice>>
[
sum
]
map-sum
]
[
end-slice>>
sum
]
bi
+
;