From e47ecb822f6bb10c196ca00030b0e6f5e176495e Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Wed, 30 Dec 2020 03:45:19 +0000 Subject: [PATCH] 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. --- README.md | 5 ++-- dash.el | 18 ++++++------ dash.info | 78 +++++++++++++++++++++++++------------------------ dash.texi | 8 +++-- dev/examples.el | 8 +++-- 5 files changed, 64 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 6bb5894..3432319 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/dash.el b/dash.el index 58cbdef..d57feed 100644 --- a/dash.el +++ b/dash.el @@ -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. diff --git a/dash.info b/dash.info index 22bebe4..1e6a14b 100644 --- a/dash.info +++ b/dash.info @@ -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 diff --git a/dash.texi b/dash.texi index c6864ee..8a3b73b 100644 --- a/dash.texi +++ b/dash.texi @@ -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 diff --git a/dev/examples.el b/dev/examples.el index 4997f5b..04e766f 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -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))