branch: externals/dash
commit 8af9987aac0352edabdf504c070837d89ee29016
Author: Paul Pogonyshev <pogonys...@gmail.com>
Commit: Matus Goljer <matus.gol...@gmail.com>

    Add '-each-r' and '-each-r-while'.
---
 dash.el         | 38 ++++++++++++++++++++++++++++++++++++++
 dev/examples.el | 10 ++++++++++
 2 files changed, 48 insertions(+)

diff --git a/dash.el b/dash.el
index ad6ab65..33cf56b 100644
--- a/dash.el
+++ b/dash.el
@@ -125,6 +125,44 @@ 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")))
+    `(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/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))

Reply via email to