Improve --each-while

* dash.el (--each-while): Optimize and avoid unused lexical variable
byte-compiler warnings.
* dev/examples.el (-each-while): Add more tests.
* README.md:
* dash.info:
* dash.texi: Regenerate docs.
master
Basil L. Contovounesios 5 years ago
parent f800e2e653
commit e47ecb822f
No known key found for this signature in database
GPG Key ID: 205AB54A5D5D8CFF
  1. 5
      README.md
  2. 18
      dash.el
  3. 78
      dash.info
  4. 8
      dash.texi
  5. 8
      dev/examples.el

@ -2571,8 +2571,9 @@ Call `fn` with every item in `list` while (`pred` item) is non-nil.
Return nil, used for side-effects only.
```el
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s) ;; => '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s) ;; => '(2 1)
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (push item s))) s) ;; => '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (push it s)) s) ;; => '(2 1)
(let ((s 0)) (--each-while '(1 3 4 5) (odd? it) (setq s (+ s it))) s) ;; => 4
```
#### -each-indexed `(list fn)`

@ -109,16 +109,16 @@ See also: `-map-indexed'."
"Anaphoric form of `-each-while'."
(declare (debug (form form body))
(indent 2))
(let ((l (make-symbol "list"))
(c (make-symbol "continue")))
(let ((l (make-symbol "list")))
`(let ((,l ,list)
(,c t)
(it-index 0))
(while (and ,l ,c)
(let ((it (car ,l)))
(if (not ,pred) (setq ,c nil) ,@body))
(setq it-index (1+ it-index))
(!cdr ,l)))))
(it-index 0)
it)
(ignore it)
(while (when ,l
(setq it (pop ,l))
,pred)
,@body
(setq it-index (1+ it-index))))))
(defun -each-while (list pred fn)
"Call FN with every item in LIST while (PRED item) is non-nil.

@ -2485,10 +2485,12 @@ Functions iterating over lists for side-effect only.
Call FN with every item in LIST while (PRED item) is non-nil.
Return nil, used for side-effects only.
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s)
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (push item s))) s)
⇒ '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s)
(let (s) (--each-while '(1 2 3 4) (< it 3) (push it s)) s)
⇒ '(2 1)
(let ((s 0)) (--each-while '(1 3 4 5) (odd? it) (setq s (+ s it))) s)
⇒ 4
-- Function: -each-indexed (list fn)
Call (FN index item) for each item in LIST.
@ -3003,7 +3005,7 @@ Index
* !cons: Destructive operations.
(line 6)
* -->: Threading macros. (line 32)
* --doto: Side-effects. (line 81)
* --doto: Side-effects. (line 83)
* ->: Threading macros. (line 6)
* ->>: Threading macros. (line 19)
* -all?: Predicates. (line 18)
@ -3035,15 +3037,15 @@ Index
(line 168)
* -difference: Set operations. (line 20)
* -distinct: Set operations. (line 62)
* -dotimes: Side-effects. (line 61)
* -doto: Side-effects. (line 70)
* -dotimes: Side-effects. (line 63)
* -doto: Side-effects. (line 72)
* -drop: Sublist selection. (line 125)
* -drop-last: Sublist selection. (line 137)
* -drop-while: Sublist selection. (line 158)
* -each: Side-effects. (line 8)
* -each-indexed: Side-effects. (line 28)
* -each-r: Side-effects. (line 41)
* -each-r-while: Side-effects. (line 52)
* -each-indexed: Side-effects. (line 30)
* -each-r: Side-effects. (line 43)
* -each-r-while: Side-effects. (line 54)
* -each-while: Side-effects. (line 19)
* -elem-index: Indexing. (line 9)
* -elem-indices: Indexing. (line 21)
@ -3393,36 +3395,36 @@ Ref: -setq82691
Node: Side-effects83507
Ref: -each83701
Ref: -each-while84108
Ref: -each-indexed84468
Ref: -each-r84986
Ref: -each-r-while85419
Ref: -dotimes85794
Ref: -doto86097
Ref: --doto86524
Node: Destructive operations86799
Ref: !cons86972
Ref: !cdr87178
Node: Function combinators87373
Ref: -partial87647
Ref: -rpartial88040
Ref: -juxt88442
Ref: -compose88874
Ref: -applify89432
Ref: -on89863
Ref: -flip90389
Ref: -const90701
Ref: -cut91045
Ref: -not91531
Ref: -orfn91841
Ref: -andfn92275
Ref: -iteratefn92770
Ref: -fixfn93473
Ref: -prodfn95042
Node: Development96111
Node: Contribute96460
Node: Changes97208
Node: Contributors100207
Node: Index101831
Ref: -each-indexed84566
Ref: -each-r85084
Ref: -each-r-while85517
Ref: -dotimes85892
Ref: -doto86195
Ref: --doto86622
Node: Destructive operations86897
Ref: !cons87070
Ref: !cdr87276
Node: Function combinators87471
Ref: -partial87745
Ref: -rpartial88138
Ref: -juxt88540
Ref: -compose88972
Ref: -applify89530
Ref: -on89961
Ref: -flip90487
Ref: -const90799
Ref: -cut91143
Ref: -not91629
Ref: -orfn91939
Ref: -andfn92373
Ref: -iteratefn92868
Ref: -fixfn93571
Ref: -prodfn95140
Node: Development96209
Node: Contribute96558
Node: Changes97306
Node: Contributors100305
Node: Index101929

End Tag Table

@ -3881,13 +3881,17 @@ Return nil, used for side-effects only.
@example
@group
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s)
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (push item s))) s)
@result{} '(4 2)
@end group
@group
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s)
(let (s) (--each-while '(1 2 3 4) (< it 3) (push it s)) s)
@result{} '(2 1)
@end group
@group
(let ((s 0)) (--each-while '(1 3 4 5) (odd? it) (setq s (+ s it))) s)
@result{} 4
@end group
@end example
@end defun

@ -1280,8 +1280,12 @@ new list."
(let (s) (--each (reverse (three-letters)) (setq s (cons it s))) s) => '("A" "B" "C"))
(defexamples -each-while
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s) => '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s) => '(2 1))
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (push item s))) s) => '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (push it s)) s) => '(2 1)
(let ((s 0)) (--each-while '(1 3 4 5) (odd? it) (setq s (+ s it))) s) => 4
(let (s) (--each-while () t (setq s t)) s) => nil
(let (s) (--each-while '(1) t (setq s it)) s) => 1
(let (s) (--each-while '(1) nil (setq s it)) s) => nil)
(defexamples -each-indexed
(let (s) (-each-indexed '(a b c) (lambda (index item) (setq s (cons (list item index) s)))) s) => '((c 2) (b 1) (a 0))

Loading…
Cancel
Save