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

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

[elpa] externals/dash 4e76865 075/426: Add !take-while


From: Phillip Lord
Subject: [elpa] externals/dash 4e76865 075/426: Add !take-while
Date: Tue, 04 Aug 2015 19:36:48 +0000

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

    Add !take-while
---
 README.md   |   11 +++++++++++
 bang.el     |   15 +++++++++++++++
 examples.el |    5 +++++
 3 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index 5441fd2..15da0c9 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,7 @@ Or you can just dump `bang.el` in your load path somewhere.
 * [!keep](#keep-fn-list) `(fn list)`
 * [!concat](#concat-rest-lists) `(&rest lists)`
 * [!mapcat](#mapcat-fn-list) `(fn list)`
+* [!take-while](#take-while-fn-list) `(fn list)`
 * [!interpose](#interpose-sep-list) `(sep list)`
 * [!replace-where](#replace-where-pred-rep-list) `(pred rep list)`
 * [!first](#first-fn-list) `(fn list)`
@@ -162,6 +163,16 @@ Thus function `fn` should return a collection.
 (!!mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3)
 ```
 
+### !take-while `(fn list)`
+
+Returns a new list of successive items from `list` while (`fn` item) returns a 
non-nil value.
+
+```cl
+(!take-while 'even? '(1 2 3 4)) ;; => '()
+(!take-while 'even? '(2 4 5 6)) ;; => '(2 4)
+(!!take-while (< it 4) '(1 2 3 4 3 2 1)) ;; => '(1 2 3)
+```
+
 ### !interpose `(sep list)`
 
 Returns a new list of all elements in `list` separated by `sep`.
diff --git a/bang.el b/bang.el
index 4f5c404..4fc290f 100644
--- a/bang.el
+++ b/bang.el
@@ -144,6 +144,21 @@ the supplied LISTS."
 Thus function FN should return a collection."
   (!!mapcat (funcall fn it) list))
 
+(defmacro !!take-while (form list)
+  "Anaphoric form of `!take-while'."
+  (let ((l (make-symbol "list"))
+        (r (make-symbol "result")))
+    `(let ((,l ,list)
+           (,r '()))
+       (while (and ,l (let ((it (car ,l))) ,form))
+         (setq ,r (cons (car ,l) ,r))
+         (setq ,l (cdr ,l)))
+       (nreverse ,r))))
+
+(defun !take-while (fn list)
+  "Returns a new list of successive items from LIST while (FN item) returns a 
non-nil value."
+  (!!take-while (funcall fn it) list))
+
 (defun !interpose (sep list)
   "Returns a new list of all elements in LIST separated by SEP."
   (let (result)
diff --git a/examples.el b/examples.el
index d894a4d..32f9221 100644
--- a/examples.el
+++ b/examples.el
@@ -58,6 +58,11 @@
   (!mapcat (lambda (item) (list 0 item)) '(1 2 3)) => '(0 1 0 2 0 3)
   (!!mapcat (list 0 it) '(1 2 3)) => '(0 1 0 2 0 3))
 
+(defexamples !take-while
+  (!take-while 'even? '(1 2 3 4)) => '()
+  (!take-while 'even? '(2 4 5 6)) => '(2 4)
+  (!!take-while (< it 4) '(1 2 3 4 3 2 1)) => '(1 2 3))
+
 (defexamples !interpose
   (!interpose "-" '()) => '()
   (!interpose "-" '("a")) => '("a")



reply via email to

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