left-fold given collection returning accumulator
Parameters
fold collection :integer :string :dictionary :object :inline :block :range
params :null :block
action :block :bytecode
Attributes
with | :literal | use given index |
seed | :any | use specific seed value |
right | | perform right folding |
Returns
Examples
copy
fold 1..10 [x,y]-> x + y
; => 55 (1+2+3+4..)
fold 1..10 .seed:1 [x,y][ x * y ]
; => 3628800 (10!)
copy
fold 1..3 [x,y]-> x - y
; => -6
fold.right 1..3 [x,y]-> x - y
; => 2
copy
fold.seed:"0" to [:string] 1..5 [x,y] ->
"(" ++ x ++ "+" ++ y ++ ")"
; => (((((0+1)+2)+3)+4)+5)
fold.right.seed:"0" to [:string] 1..5 [x,y] ->
"(" ++ x ++ "+" ++ y ++ ")"
; => (1+(2+(3+(4+(5+0)))))
copy
fold 1..10 [x y z] [
print [x y z]
x + z - y
]
; 0 1 2
; 1 3 4
; 2 5 6
; 3 7 8
; 4 9 10
; => 5
copy
fold.with:'i 1..5 [x y][
print [i x y]
i * x+y
]
; 0 0 1
; 1 0 2
; 2 2 3
; 3 10 4
; 4 42 5
; => 188
Related