Basic Examples (7)
Apply three functions (f, g and h) and three elements (a, b and c) to a seed (x):
Use the 2-argument form to get the same result:
If the number of functions is less than the number of elements, the functions will "rotate" round to fill out:
If the number of functions is greater than the number of elements, some may not be used:
Using an empty list of functions will return the list of elements along with the seed:
Using an empty list of elements and no seed will return an empty list:
Adding a seed effectively prevents the second list from being registered as empty:
Scope (5)
FoldRotate can take an arbitrary number of functions or elements:
FoldRotate can take pure functions. The first slot (#1) refers to the current value (starting with the seed); the second slot (#2) refers to the element being folded in:
Use Sow and Reap to get intermediary results (similar to how FoldList gets intermediary results for Fold):
An iterator and piecewise function can be used in place of multiple functions:
FoldRotate can be exited early with Throw. Use FoldRotate (along with Sow and Reap) to generate the nth partial sum in the series 1 + 2 × 3 + 4 × 5 + …:
Exit early when the partial sum is a multiple of 7:
Applications (1)
Calculate alternating totals of a list:
Properties and Relations (3)
FoldRotate can be used to mimic the behaviour of Fold:
In fact, if only one function is provided, FoldRotate (in its 3-argument form) reduces to Fold:
In order to return an unevaluated expression, Defer (or HoldForm) must be used at every level:
Evaluate the expression with a replacement rule:
FoldRotate is generally slower than Fold, especially when using large number of functions:
Possible Issues (3)
FoldRotate takes either two or three arguments:
The first and last arguments must be lists:
Given no arguments at all, FoldRotate will not evaluate:
Neat Examples (1)
Create a nesting of frames cycling through three colours: