emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 85e8f62 119/316: Merge pull request #274 from magn


From: ELPA Syncer
Subject: [elpa] externals/dash 85e8f62 119/316: Merge pull request #274 from magnars/doublep-each-r
Date: Mon, 15 Feb 2021 15:57:39 -0500 (EST)

branch: externals/dash
commit 85e8f62b7a8ae0b4da307ddf16e4f1c3559d0d3f
Merge: 453c775 14f76df
Author: Matus Goljer <dota.keys@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #274 from magnars/doublep-each-r
    
    Doublep each r
---
 README.md          | 24 ++++++++++++++++
 dash.el            | 43 +++++++++++++++++++++++++++++
 dash.info          | 80 +++++++++++++++++++++++++++++++++++-------------------
 dash.texi          | 38 ++++++++++++++++++++++++++
 dev/examples.el    | 10 +++++++
 readme-template.md |  1 +
 6 files changed, 168 insertions(+), 28 deletions(-)

diff --git a/README.md b/README.md
index 1070672..aa9deeb 100644
--- a/README.md
+++ b/README.md
@@ -298,6 +298,8 @@ Functions iterating over lists for side-effect only.
 * [-each](#-each-list-fn) `(list fn)`
 * [-each-while](#-each-while-list-pred-fn) `(list pred fn)`
 * [-each-indexed](#-each-indexed-list-fn) `(list fn)`
+* [-each-r](#-each-r-list-fn) `(list fn)`
+* [-each-r-while](#-each-r-while-list-pred-fn) `(list pred fn)`
 * [-dotimes](#-dotimes-num-fn) `(num fn)`
 * [-doto](#-doto-eval-initial-value-rest-forms) `(eval-initial-value &rest 
forms)`
 
@@ -2531,6 +2533,27 @@ See also: [`-map-indexed`](#-map-indexed-fn-list).
 (let (s) (--each-indexed '(a b c) (setq s (cons (list it it-index) s))) s) ;; 
=> '((c 2) (b 1) (a 0))
 ```
 
+#### -each-r `(list fn)`
+
+Call `fn` with every item in `list` in reversed order.
+ Return nil, used for side-effects only.
+
+```el
+(let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s))))) ;; => nil
+(let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))) s) ;; => 
'(1 2 3)
+(let (s) (--each-r '(1 2 3) (setq s (cons it s))) s) ;; => '(1 2 3)
+```
+
+#### -each-r-while `(list pred fn)`
+
+Call `fn` with every item in reversed `list` while (`pred` item) is non-nil.
+Return nil, used for side-effects only.
+
+```el
+(let (s) (-each-r-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s) 
;; => '(6)
+(let (s) (--each-r-while '(1 2 3 4) (>= it 3) (!cons it s)) s) ;; => '(3 4)
+```
+
 #### -dotimes `(num fn)`
 
 Repeatedly calls `fn` (presumably for side-effects) passing in integers from 0 
through `num-1`.
@@ -3001,6 +3024,7 @@ things compatible but no future guarantees are made.
  - [William West](https://github.com/occidens) made `-fixfn` more robust at 
handling floats.
  - [Cam Saül](https://github.com/camsaul) contributed `-some->`, `-some->>`, 
and `-some-->`.
  - [Basil L. Contovounesios](https://github.com/basil-conto) contributed 
`-common-prefix`.
+ - [Paul Pogonyshev](https://github.com/doublep) contributed `-each-r` and 
`-each-r-while`.
 
 Thanks!
 
diff --git a/dash.el b/dash.el
index ad6ab65..8d1d9a6 100644
--- a/dash.el
+++ b/dash.el
@@ -125,6 +125,49 @@ Return nil, used for side-effects only."
 
 (put '-each-while 'lisp-indent-function 2)
 
+(defmacro --each-r (list &rest body)
+  "Anaphoric form of `-each-r'."
+  (declare (debug (form body))
+           (indent 1))
+  (let ((v (make-symbol "vector")))
+    ;; Implementation note: building vector is considerably faster
+    ;; than building a reversed list (vector takes less memory, so
+    ;; there is less GC), plus length comes naturally.  In-place
+    ;; 'nreverse' would be faster still, but BODY would be able to see
+    ;; that, even if modification was reversed before we return.
+    `(let* ((,v (vconcat ,list))
+            (it-index (length ,v))
+            it)
+       (while (> it-index 0)
+         (setq it-index (1- it-index))
+         (setq it (aref ,v it-index))
+         ,@body))))
+
+(defun -each-r (list fn)
+  "Call FN with every item in LIST in reversed order.
+ Return nil, used for side-effects only."
+  (--each-r list (funcall fn it)))
+
+(defmacro --each-r-while (list pred &rest body)
+  "Anaphoric form of `-each-r-while'."
+  (declare (debug (form form body))
+           (indent 2))
+  (let ((v (make-symbol "vector")))
+    `(let* ((,v (vconcat ,list))
+            (it-index (length ,v))
+            it)
+       (while (> it-index 0)
+         (setq it-index (1- it-index))
+         (setq it (aref ,v it-index))
+         (if (not ,pred)
+             (setq it-index -1)
+           ,@body)))))
+
+(defun -each-r-while (list pred fn)
+  "Call FN with every item in reversed LIST while (PRED item) is non-nil.
+Return nil, used for side-effects only."
+  (--each-r-while list (funcall pred it) (funcall fn it)))
+
 (defmacro --dotimes (num &rest body)
   "Repeatedly executes BODY (presumably for side-effects) with symbol `it' 
bound to integers from 0 through NUM-1."
   (declare (debug (form body))
diff --git a/dash.info b/dash.info
index e92ab33..4927f60 100644
--- a/dash.info
+++ b/dash.info
@@ -2455,6 +2455,26 @@ Functions iterating over lists for side-effect only.
           (let (s) (--each-indexed '(a b c) (setq s (cons (list it it-index) 
s))) s)
               ⇒ '((c 2) (b 1) (a 0))
 
+ -- Function: -each-r (list fn)
+     Call FN with every item in LIST in reversed order.  Return nil,
+     used for side-effects only.
+
+          (let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))))
+              ⇒ nil
+          (let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))) s)
+              ⇒ '(1 2 3)
+          (let (s) (--each-r '(1 2 3) (setq s (cons it s))) s)
+              ⇒ '(1 2 3)
+
+ -- Function: -each-r-while (list pred fn)
+     Call FN with every item in reversed LIST while (PRED item) is
+     non-nil.  Return nil, used for side-effects only.
+
+          (let (s) (-each-r-while '(2 4 5 6) 'even? (lambda (item) (!cons item 
s))) s)
+              ⇒ '(6)
+          (let (s) (--each-r-while '(1 2 3 4) (>= it 3) (!cons it s)) s)
+              ⇒ '(3 4)
+
  -- Function: -dotimes (num fn)
      Repeatedly calls FN (presumably for side-effects) passing in
      integers from 0 through NUM-1.
@@ -2958,13 +2978,15 @@ Index
                                                             (line 139)
 * -difference:                           Set operations.    (line  20)
 * -distinct:                             Set operations.    (line  62)
-* -dotimes:                              Side-effects.      (line  41)
-* -doto:                                 Side-effects.      (line  50)
+* -dotimes:                              Side-effects.      (line  61)
+* -doto:                                 Side-effects.      (line  70)
 * -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-while:                           Side-effects.      (line  19)
 * -elem-index:                           Indexing.          (line   9)
 * -elem-indices:                         Indexing.          (line  21)
@@ -3311,32 +3333,34 @@ Node: Side-effects82009
 Ref: -each82203
 Ref: -each-while82610
 Ref: -each-indexed82970
-Ref: -dotimes83488
-Ref: -doto83791
-Node: Destructive operations84218
-Ref: !cons84391
-Ref: !cdr84597
-Node: Function combinators84792
-Ref: -partial85066
-Ref: -rpartial85461
-Ref: -juxt85863
-Ref: -compose86295
-Ref: -applify86853
-Ref: -on87300
-Ref: -flip87823
-Ref: -const88135
-Ref: -cut88479
-Ref: -not88965
-Ref: -orfn89275
-Ref: -andfn89709
-Ref: -iteratefn90204
-Ref: -fixfn90907
-Ref: -prodfn92476
-Node: Development93542
-Node: Contribute93891
-Node: Changes94639
-Node: Contributors97638
-Node: Index99262
+Ref: -each-r83488
+Ref: -each-r-while83921
+Ref: -dotimes84296
+Ref: -doto84599
+Node: Destructive operations85026
+Ref: !cons85199
+Ref: !cdr85405
+Node: Function combinators85600
+Ref: -partial85874
+Ref: -rpartial86269
+Ref: -juxt86671
+Ref: -compose87103
+Ref: -applify87661
+Ref: -on88108
+Ref: -flip88631
+Ref: -const88943
+Ref: -cut89287
+Ref: -not89773
+Ref: -orfn90083
+Ref: -andfn90517
+Ref: -iteratefn91012
+Ref: -fixfn91715
+Ref: -prodfn93284
+Node: Development94350
+Node: Contribute94699
+Node: Changes95447
+Node: Contributors98446
+Node: Index100070
 
 End Tag Table
 
diff --git a/dash.texi b/dash.texi
index 2243a34..9a4ea91 100644
--- a/dash.texi
+++ b/dash.texi
@@ -3837,6 +3837,44 @@ See also: @code{-map-indexed} (@pxref{-map-indexed}).
 @end example
 @end defun
 
+@anchor{-each-r}
+@defun -each-r (list fn)
+Call @var{fn} with every item in @var{list} in reversed order.
+ Return nil, used for side-effects only.
+
+@example
+@group
+(let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))))
+    @result{} nil
+@end group
+@group
+(let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))) s)
+    @result{} '(1 2 3)
+@end group
+@group
+(let (s) (--each-r '(1 2 3) (setq s (cons it s))) s)
+    @result{} '(1 2 3)
+@end group
+@end example
+@end defun
+
+@anchor{-each-r-while}
+@defun -each-r-while (list pred fn)
+Call @var{fn} with every item in reversed @var{list} while (@var{pred} item) 
is non-nil.
+Return nil, used for side-effects only.
+
+@example
+@group
+(let (s) (-each-r-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s)
+    @result{} '(6)
+@end group
+@group
+(let (s) (--each-r-while '(1 2 3 4) (>= it 3) (!cons it s)) s)
+    @result{} '(3 4)
+@end group
+@end example
+@end defun
+
 @anchor{-dotimes}
 @defun -dotimes (num fn)
 Repeatedly calls @var{fn} (presumably for side-effects) passing in integers 
from 0 through @var{num-1}.
diff --git a/dev/examples.el b/dev/examples.el
index a3c7393..05669e9 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -1214,6 +1214,16 @@ new list."
     (let (s) (-each-indexed '(a b c) (lambda (index item) (setq s (cons (list 
item index) s)))) s) => '((c 2) (b 1) (a 0))
     (let (s) (--each-indexed '(a b c) (setq s (cons (list it it-index) s))) s) 
=> '((c 2) (b 1) (a 0)))
 
+  (defexamples -each-r
+    (let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s))))) => nil
+    (let (s) (-each-r '(1 2 3) (lambda (item) (setq s (cons item s)))) s) => 
'(1 2 3)
+    (let (s) (--each-r '(1 2 3) (setq s (cons it s))) s) => '(1 2 3)
+    (let (s) (--each-r (reverse (three-letters)) (setq s (cons it s))) s) => 
'("C" "B" "A"))
+
+  (defexamples -each-r-while
+    (let (s) (-each-r-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) 
s) => '(6)
+    (let (s) (--each-r-while '(1 2 3 4) (>= it 3) (!cons it s)) s) => '(3 4))
+
   (defexamples -dotimes
     (let (s) (-dotimes 3 (lambda (n) (!cons n s))) s) => '(2 1 0)
     (let (s) (--dotimes 5 (!cons it s)) s) => '(4 3 2 1 0))
diff --git a/readme-template.md b/readme-template.md
index 9643a46..7ffbe36 100644
--- a/readme-template.md
+++ b/readme-template.md
@@ -265,6 +265,7 @@ things compatible but no future guarantees are made.
  - [William West](https://github.com/occidens) made `-fixfn` more robust at 
handling floats.
  - [Cam Saül](https://github.com/camsaul) contributed `-some->`, `-some->>`, 
and `-some-->`.
  - [Basil L. Contovounesios](https://github.com/basil-conto) contributed 
`-common-prefix`.
+ - [Paul Pogonyshev](https://github.com/doublep) contributed `-each-r` and 
`-each-r-while`.
 
 Thanks!
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]