Rebol [ Title: "Accumulate" Purpose: {The ACCUM function} File: %accum.r Date: 3-Jun-2009/11:36:13+2:00 Author: "Ladislav Mecir" ] use [return2] [ return2: func [ { special return function using the given ATTRIBUTE value to adjust the spec of the ACCUMULATE function } [throw] value [any-type!] attribute ] [ poke third :accumulate 2 attribute return get/any 'value ] accumulate: func [ {Evaluates a block, accumulating values via UNIFY function. Returns the accumulated value.} [throw] a [any-type!] body [block!] unify [any-function!] /local attribute ] [ attribute: [throw] return2 do func [unify] compose/only [ do (:body) attribute: {no-attribute} return get/any 'a ] func [[throw] b [any-type!]] compose [ error? set/any 'a (:unify) get/any 'a get/any 'b ] attribute ] ] #do [i-comment [ accumulate 0.0 [foreach i [1 2 3 4 5] [unify i]] :add ; == 15.0 accumulate 1.0 [foreach i [1 2 3 4 5] [unify i]] :multiply ; == 120.0 head accumulate copy [] [foreach i [1 2 3 4 5] [unify i]] :insert ; == [1 2 3 4 5] ]]