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

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

[elpa] externals/dash c218e9f 150/426: -split-at/with now makes just one


From: Phillip Lord
Subject: [elpa] externals/dash c218e9f 150/426: -split-at/with now makes just one pass over list
Date: Tue, 04 Aug 2015 19:37:20 +0000

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

    -split-at/with now makes just one pass over list
---
 README.md |    6 +++---
 dash.el   |   31 +++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index 0c392c6..fcedcbc 100644
--- a/README.md
+++ b/README.md
@@ -358,7 +358,7 @@ Returns the tail of `list` starting from the first item for 
which (`pred` item)
 
 ### -split-at `(n list)`
 
-Returns a list of ((-take `n` `list`) (-drop `n` `list`))
+Returns a list of ((-take `n` `list`) (-drop `n` `list`)), in no more than one 
pass through the list.
 
 ```cl
 (-split-at 3 '(1 2 3 4 5)) ;; => '((1 2 3) (4 5))
@@ -367,7 +367,7 @@ Returns a list of ((-take `n` `list`) (-drop `n` `list`))
 
 ### -split-with `(pred list)`
 
-Returns a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`))
+Returns a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`)), 
in no more than one pass through the list.
 
 ```cl
 (-split-with 'even? '(1 2 3 4)) ;; => '(nil (1 2 3 4))
@@ -377,7 +377,7 @@ Returns a list of ((-take-while `pred` `list`) (-drop-while 
`pred` `list`))
 
 ### -separate `(pred list)`
 
-Returns a list of ((-filter `pred` `list`) (-remove `pred` `list`)).
+Returns a list of ((-filter `pred` `list`) (-remove `pred` `list`)), in one 
pass through the list.
 
 ```cl
 (-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) ;; => '((2 4 6) (1 
3 5 7))
diff --git a/dash.el b/dash.el
index 5694e13..6ac7310 100644
--- a/dash.el
+++ b/dash.el
@@ -358,17 +358,32 @@ Returns `nil` both if all items match the predicate, and 
if none of the items ma
   (--drop-while (funcall pred it) list))
 
 (defun -split-at (n list)
-  "Returns a list of ((-take N LIST) (-drop N LIST))"
-  (list (-take n list)
-        (-drop n list)))
+  "Returns a list of ((-take N LIST) (-drop N LIST)), in no more than one pass 
through the list."
+  (let (result)
+    (--dotimes n
+      (when list
+        (!cons (car list) result)
+        (!cdr list)))
+    (list (nreverse result) list)))
 
-(defmacro --split-with (form list)
+(defmacro --split-with (pred list)
   "Anaphoric form of `-split-with'."
-  `(list (--take-while ,form ,list)
-         (--drop-while ,form ,list)))
+  (let ((l (make-symbol "list"))
+        (r (make-symbol "result"))
+        (c (make-symbol "continue")))
+    `(let ((,l ,list)
+           (,r nil)
+           (,c t))
+       (while (and ,l ,c)
+         (let ((it (car ,l)))
+           (if (not ,pred)
+               (setq ,c nil)
+             (!cons it ,r)
+             (!cdr ,l))))
+       (list (nreverse ,r) ,l))))
 
 (defun -split-with (pred list)
-  "Returns a list of ((-take-while PRED LIST) (-drop-while PRED LIST))"
+  "Returns a list of ((-take-while PRED LIST) (-drop-while PRED LIST)), in no 
more than one pass through the list."
   (--split-with (funcall pred it) list))
 
 (defmacro --separate (form list)
@@ -380,7 +395,7 @@ Returns `nil` both if all items match the predicate, and if 
none of the items ma
        (list (nreverse ,y) (nreverse ,n)))))
 
 (defun -separate (pred list)
-  "Returns a list of ((-filter PRED LIST) (-remove PRED LIST))."
+  "Returns a list of ((-filter PRED LIST) (-remove PRED LIST)), in one pass 
through the list."
   (--separate (funcall pred it) list))
 
 (defun -partition (n list)



reply via email to

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