Wolfram: Scan (foreach)
Map but return Null (aka Foreach)
Scan[f, list]-
Same as
Map, but returnNull. 〔see Wolfram: Map Function to List〕Scanallows the use ofReturnandThrowto exit and return a alternative value.xx = {a, {{b,c}, d}, e}; (* map to leafs, but as soon as c is found, exit. *) Scan[ Function[{x}, Print[ x ]; If[x === c, Return[x] ]], xx, {-1} ] (* a b c Null *)
💡 TIP: When to Use Scan
Scan is useful when:
- You want to go over every element, but do not need the return value. In JavaScript known as “foreach”. 〔see JS: Array.prototype.forEach〕
- You want to go over every element, but as soon as a condition is found, exit.
Scanallows you to map a function to leafs of a tree or other levels of nodes, using levelspec (likeMap), but as soon as a condition is found, exit.
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