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

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

[elpa] externals/dash db8a745 056/439: Add !each


From: Phillip Lord
Subject: [elpa] externals/dash db8a745 056/439: Add !each
Date: Tue, 04 Aug 2015 20:26:20 +0000

branch: externals/dash
commit db8a7454dcf5db1e941845359adca7d97b1065eb
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>

    Add !each
---
 README.md   |   11 +++++++++++
 bang.el     |   12 ++++++++++++
 examples.el |    5 +++++
 3 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index f987e4f..e8855bd 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,7 @@ This is so much a work in progress that you should definitely 
not be using it ye
 * [!contains?](#contains-list-element) `(list element)`
 * [!some](#some-fn-list) `(fn list)`
 * [!every?](#every-fn-list) `(fn list)`
+* [!each](#each-list-fn) `(list fn)`
 
 There are also anaphoric versions of these functions where that makes sense,
 prefixed with two bangs instead of one.
@@ -227,6 +228,16 @@ Returns t if (`fn` x) is non-nil for every x in `list`, 
else nil.
 (!!every? (= 0 (% it 2)) '(2 4 6)) ;; => t
 ```
 
+### !each `(list fn)`
+
+Calls `fn` with every item in `list`. Returns nil, used for side-effects only.
+
+```cl
+(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s))))) ;; => nil
+(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s)))) s) ;; => '(3 
2 1)
+(let (s) (!!each '(1 2 3) (setq s (cons it s))) s) ;; => '(3 2 1)
+```
+
 
 ## Development
 
diff --git a/bang.el b/bang.el
index 8e01b94..1aedf30 100644
--- a/bang.el
+++ b/bang.el
@@ -196,6 +196,18 @@ or with `!compare-fn' if that's non-nil."
   "Returns t if (FN x) is non-nil for every x in LIST, else nil."
   (!!every? (funcall fn it) list))
 
+(defmacro !!each (list form)
+  "Anaphoric form of `!each'."
+  `(let ((!--list ,list))
+     (while !--list
+       (let ((it (car !--list)))
+         ,form)
+       (setq !--list (cdr !--list)))))
+
+(defun !each (list fn)
+  "Calls FN with every item in LIST. Returns nil, used for side-effects only."
+  (!!each list (funcall fn it)))
+
 (defvar !compare-fn nil
   "Tests for equality use this function or `equal' if this is nil.
 It should only be set using dynamic scope with a let, like:
diff --git a/examples.el b/examples.el
index 6593cc6..df6ae81 100644
--- a/examples.el
+++ b/examples.el
@@ -90,3 +90,8 @@
   (!every? 'even? '(1 2 3)) => nil
   (!every? 'even? '(2 4 6)) => t
   (!!every? (= 0 (% it 2)) '(2 4 6)) => t)
+
+(defexamples !each
+  (let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s))))) => nil
+  (let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s)))) s) => '(3 2 
1)
+  (let (s) (!!each '(1 2 3) (setq s (cons it s))) s) => '(3 2 1))



reply via email to

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