"FoldList" (Incremental Object)
IncrementalObject["FoldList"] [f,init,{e1,e2,…}]
creates an object that incrementally returns values that are successive applications of the binary operation f to the seed init and the elements ei.
IncrementalObject["FoldList"] [f,init,inc]
creates an object that incrementally returns values from the incremental object or incremental function inc that are successive applications of the binary operation f to the seed init and the elements ei.
Details
- A "FoldList" incremental object is useful to accumulate results that come from the elements of an expression.
- The expression used as a source can be an object with any head, not necessarily List.
- The expression inc can be an expression with head IncrementalObject.
- The expression inc can be an object created by compiled code that uses IncrementalFunction.
- The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds InputForm[ds] input form of ds
Examples
open all close allBasic Examples (1)
Scope (4)
The expression argument does not have to be a list, just any compound expression:
obj = IncrementalObject["FoldList"][#2&, x, f[a[1], a[1, 2]]];NextValue[obj]NextValue[obj]There are no more values, and a failure object is returned:
NextValue[obj]An incremental object can be used in Table:
obj = IncrementalObject["FoldList"][Plus, 0, Range[10]];
Table[e, {e, obj}]An incremental object can be used in Do:
obj = IncrementalObject["FoldList"][Max, 0, {1, 3, 7, 6, 2, 8, 11}];
Do[Echo[e], {e, obj}]"FoldList" incremental objects can use an IncrementalObject as a source:
iobj = IncrementalObject["Range"][8];Create an incremental object using the incremental object as a source:
obj = IncrementalObject["FoldList"][Plus, 0, iobj]A list of all the values from the incremental object:
Table[e, {e, obj}]"FoldList" incremental objects can use an IncrementalFunction as a source:
ifun = FunctionCompile[IncrementalFunction[Function[{}, Do[IncrementalYield[i], {i, Infinity}]]]][];Create an incremental object using the incremental function as a source:
obj = IncrementalObject["FoldList"][Plus, 0, ifun]The first 20 results from the incremental object:
Table[NextValue[obj], 20]Properties & Relations (2)
InputForm (1)
InputForm returns the serialized contents of a "FoldList" incremental object:
InputForm[IncrementalObject["FoldList"][f, x, {1, 2, 3}]]This serialized form can be used to recreate the incremental object:
IncrementalObject["FoldList", {"Function" :> f, "State" :> x, "Source" :> IncrementalObject["Identity", {"Source" :> {1, 2, 3}, "Position" :> 1}]}]SameQ (1)
SameQ can be used to test whether two incremental objects contain the same sequence at the same position:
obj1 = IncrementalObject["FoldList"][f, 1, {a, b, c, d}];
obj2 = IncrementalObject["FoldList"][f, 1, {a, b, c, d}];
obj1 === obj2If one objects moves to the next value, the two objects are no longer the same:
NextValue[obj1];
obj1 === obj2Advancing the other object means that they are both now the same:
NextValue[obj2];
obj1 === obj2See Also
Related Guides
History
Introduced in 2026 (15.0)