[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash a08fc14 102/426: Add -each-while
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash a08fc14 102/426: Add -each-while |
Date: |
Tue, 04 Aug 2015 19:36:59 +0000 |
branch: externals/dash
commit a08fc14da748488cebd5c87caaad3111b7244e6d
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add -each-while
---
README.md | 11 +++++++++++
dash.el | 16 ++++++++++++++++
examples.el | 4 ++++
3 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 1c309e5..15725a3 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-all?](#-all-fn-list) `(fn list)`
* [-none?](#-none-fn-list) `(fn list)`
* [-each](#-each-list-fn) `(list fn)`
+* [-each-while](#-each-while-list-pred-fn) `(list pred fn)`
* [-take](#-take-n-list) `(n list)`
* [-drop](#-drop-n-list) `(n list)`
* [-take-while](#-take-while-fn-list) `(fn list)`
@@ -227,6 +228,16 @@ Calls `fn` with every item in `list`. Returns nil, used
for side-effects only.
(let (s) (--each '(1 2 3) (setq s (cons it s))) s) ;; => '(3 2 1)
```
+### -each-while `(list pred fn)`
+
+Calls `fn` with every item in `list` while (`pred` item) is non-nil.
+Returns nil, used for side-effects only.
+
+```cl
+(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)
+```
+
### -take `(n list)`
Returns a new list of the first `n` items in `list`, or all items if there are
fewer than `n`.
diff --git a/dash.el b/dash.el
index d7ed7f1..3c38370 100644
--- a/dash.el
+++ b/dash.el
@@ -48,6 +48,22 @@
"Calls FN with every item in LIST. Returns nil, used for side-effects only."
(--each list (funcall fn it)))
+(defmacro --each-while (list pred form)
+ "Anaphoric form of `-each-while'."
+ (let ((l (make-symbol "list"))
+ (c (make-symbol "continue")))
+ `(let ((,l ,list)
+ (,c t))
+ (while (and ,l ,c)
+ (let ((it (car ,l)))
+ (if ,pred ,form (setq ,c nil)))
+ (!cdr ,l)))))
+
+(defun -each-while (list pred fn)
+ "Calls FN with every item in LIST while (PRED item) is non-nil.
+Returns nil, used for side-effects only."
+ (--each-while list (funcall pred it) (funcall fn it)))
+
(defun -map (fn list)
"Returns a new list consisting of the result of applying FN to the items in
LIST."
(mapcar fn list))
diff --git a/examples.el b/examples.el
index bbe5654..a0aca3a 100644
--- a/examples.el
+++ b/examples.el
@@ -83,6 +83,10 @@
(let (s) (--each '(1 2 3) (setq s (cons it s))) s) => '(3 2 1)
(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))
+
(defexamples -take
(-take 3 '(1 2 3 4 5)) => '(1 2 3)
(-take 17 '(1 2 3 4 5)) => '(1 2 3 4 5))
- [elpa] externals/dash 8c75026 080/426: Add !drop, (continued)
- [elpa] externals/dash 8c75026 080/426: Add !drop, Phillip Lord, 2015/08/04
- [elpa] externals/dash a2941cd 097/426: Typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash b9b6330 079/426: Add !take, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6931d8a 094/426: Remove Melpa from installation while waiting for it to be included., Phillip Lord, 2015/08/04
- [elpa] externals/dash 0bd26ac 096/426: Add aliases for -p instead of ?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38eed45 095/426: Add rainbow-dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash dc6f46c 098/426: Simplify docs-generation using dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash b0da7b2 093/426: Declare macros before using them., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4fedd2e 099/426: Add -partition and -partition-all, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79b56cc 101/426: Use --each, !cons and !cdr to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash a08fc14 102/426: Add -each-while,
Phillip Lord <=
- [elpa] externals/dash 49512b9 107/426: Simplify -take and -drop with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed47814 105/426: Add -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash eb50d17 106/426: Add Melpa back in., Phillip Lord, 2015/08/04
- [elpa] externals/dash 0e5c69b 089/426: Docs: more concise example for anaphoric functions., Phillip Lord, 2015/08/04
- [elpa] externals/dash 03370ea 108/426: Simplify examples-to-docs and examples-to-tests with dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash ba80875 109/426: Nice indentation for --each, --each-while and --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash f1c8453 083/426: Rename to dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash 51ddc13 110/426: Nice indentation for threading macros., Phillip Lord, 2015/08/04
- [elpa] externals/dash ba60707 103/426: Use --each-while to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash c56925a 111/426: Add font-lock for new functions and the `it` token., Phillip Lord, 2015/08/04