Elisp: Sequence Iteration
Sequence Iteration Guide
- Map and get all results.
mapcar
,seq-map
, etc. - Elisp: Map to Sequence
- Map but no need results. (aka foreach)
mapc
,seq-do
,seq-doseq
. - Elisp: Foreach (Side-Effect)
- Map but exit when found.
seq-some
etc. - Elisp: Sequence Iteration, Conditional Exit
- Map and insert items or delete.
seq-mapcat
. - Elisp: Sequence Map Insert
- Filter, remove items.
seq-filter
etc. - Elisp: Sequence Filter
- Find some items. Get position, count, etc.
seq-contains
etc. - Elisp: Sequence Find
- General purpose loop that pass state at each step.
seq-reduce
Reduce (Fold, Accumulator)
Reduce is a general way to do loop in functional programing style. A function is feed args from a list, and each time, the result is feed back to the function.
It solves the problem of storing value in a global variable or keeping state, such as a increment counter. By passing the state to the function.
seq-reduce
-
(seq-reduce FUNCTION SEQUENCE INITIAL-VALUE)
Recurse a function of 2 parameters x y, each time feed x the last result, feed y the new item in sequence, till no more items. Return the result.
start with putting INITIAL-VALUE in x.
For example:
(seq-reduce f [1] 0) → (f 0 1)
(seq-reduce f [1 2] 0) → (f (f 0 1) 2)
(seq-reduce f [1 2 3] 0) → (f (f (f 0 1) 2) 3)
(setq xx (vector "a" "b" "c")) (seq-reduce (lambda (x y) (concat x y)) xx "") ;; "abc"
(setq xx (vector "a" "b" "c")) (seq-reduce (lambda (x y) (concat x y)) (seq-drop xx 1) (seq-first xx)) ;; "abc"
Elisp, Loop and Iteration
Elisp, sequence functions
- Elisp: Sequence Type
- Elisp: Sequence Functions
- Elisp: Sequence, Take, Drop, Slice
- Elisp: Map to Sequence
- Elisp: Foreach (Side-Effect)
- Elisp: Sequence Iteration, Conditional Exit
- Elisp: Sequence Filter
- Elisp: Sequence Map Insert
- Elisp: Sequence Iteration
- Elisp: Sequence Find
- Elisp: Sequence, Sort, Reverse
- Elisp: Sequence Join, Convert
- Elisp: Sequence Union, Intersection, Difference
- Elisp: More Sequence Functions
- Elisp: Destructure Binding (seq-setq, seq-let)