Wolfram: Fold (reduce)
Fold (aka reduce)
Fold-
Fold[f, initValue, list]Fold[f, list]→ same asFold[f,First[list],Rest[list]]
- Recurse a function f of 2 parameters, let's call them state item.
- Each time,
- state is the last result of f.
- item is the new item in list.
- repeat untill no more items. Return the result.
- initValue is used for state when f starts.
💡 TIP: fold aka reduce, is a general purpose loop in functional programing style.
Fold[f,x,{a,b,c,d}] (* f[f[f[f[x, a], b], c], d] *) Fold[f, {a,b,c,d}] (* f[f[f[a, b], c], d] *)
FoldList
FoldList-
Like
Foldbut return a list of all steps.FoldList[f, x, {a, b, c}] (* { x, f[x, a], f[f[x, a], b], f[f[f[x, a], b], c] } *)
FoldWhile
FoldWhile-
Like
Foldbut with a condition test to stop.FoldWhile[ Function[{x,y}, x+y] , {1,1,1,1,1,1,1,1,1}, Function[x, UnsameQ[ Mod[ x, 5 ], 0 ]] ] (* 5 *) FoldWhileList-
Like
FoldWhilebut return a list of all steps.FoldWhileList[ Function[{x,y}, x+y] , {1,1,1,1,1,1,1,1,1}, Function[x, UnsameQ[ Mod[ x, 5 ], 0 ]] ] (* {1, 2, 3, 4, 5} *)
Wolfram. List Operations, and Loop, Iteration, Recursion
- Wolfram: List Operations
- Wolfram: List. Create (Table)
- Wolfram: Create Flat List (Range)
- Wolfram: List. Get Parts
- Wolfram: List. Add Element
- Wolfram: List. Delete Element
- Wolfram: List. Change Element
- Wolfram: List. Check Exist
- Wolfram: List. Join, Union, Intersection, Difference
- Wolfram: List. Min, Max
- Wolfram: List. Filter
- Wolfram: List. Sort Reverse Ordering
- Wolfram: Flatten
- Wolfram: Riffle (Add at Every Nth)
- Wolfram: RotateLeft
- Wolfram: Padding
- Wolfram: List. Partition, Reshape, Split, Gather
- Wolfram: Transpose
- Wolfram: List. Same Items Counts, Tally, Group
- Wolfram: List. Combinatorics
- Wolfram: Iteration
- Wolfram: Map Function to List
- Wolfram: Scan (foreach)
- Wolfram: Recursion
- Wolfram: Fold (reduce)
- Wolfram: Loop