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

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

[elpa] externals/dash 3c546ab 178/426: Add -partition-in-steps & -partit


From: Phillip Lord
Subject: [elpa] externals/dash 3c546ab 178/426: Add -partition-in-steps & -partition-all-in-steps
Date: Tue, 04 Aug 2015 19:37:33 +0000

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

    Add -partition-in-steps & -partition-all-in-steps
---
 README.md       |   25 +++++++++++++++++++++++++
 dash.el         |   44 +++++++++++++++++++++++++++-----------------
 dev/examples.el |   10 ++++++++++
 3 files changed, 62 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
index 82ca464..2e0b6c7 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,8 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-split-with](#-split-with-pred-list) `(pred list)`
 * [-separate](#-separate-pred-list) `(pred list)`
 * [-partition](#-partition-n-list) `(n list)`
+* [-partition-all-in-steps](#-partition-all-in-steps-n-step-list) `(n step 
list)`
+* [-partition-in-steps](#-partition-in-steps-n-step-list) `(n step list)`
 * [-partition-all](#-partition-all-n-list) `(n list)`
 * [-partition-by](#-partition-by-fn-list) `(fn list)`
 * [-partition-by-header](#-partition-by-header-fn-list) `(fn list)`
@@ -436,6 +438,29 @@ those items are discarded.
 (-partition 3 '(1 2 3 4 5 6 7)) ;; => '((1 2 3) (4 5 6))
 ```
 
+### -partition-all-in-steps `(n step list)`
+
+Returns a new list with the items in `list` grouped into `n-`sized sublists at 
offsets `step` apart.
+The last groups may contain less than `n` items.
+
+```cl
+(-partition-all-in-steps 2 1 '(1 2 3 4)) ;; => '((1 2) (2 3) (3 4) (4))
+(-partition-all-in-steps 3 2 '(1 2 3 4)) ;; => '((1 2 3) (3 4))
+(-partition-all-in-steps 3 2 '(1 2 3 4 5)) ;; => '((1 2 3) (3 4 5) (5))
+```
+
+### -partition-in-steps `(n step list)`
+
+Returns a new list with the items in `list` grouped into `n-`sized sublists at 
offsets `step` apart.
+If there are not enough items to make the last group `n-`sized,
+those items are discarded.
+
+```cl
+(-partition-in-steps 2 1 '(1 2 3 4)) ;; => '((1 2) (2 3) (3 4))
+(-partition-in-steps 3 2 '(1 2 3 4)) ;; => '((1 2 3))
+(-partition-in-steps 3 2 '(1 2 3 4 5)) ;; => '((1 2 3) (3 4 5))
+```
+
 ### -partition-all `(n list)`
 
 Returns a new list with the items in `list` grouped into `n-`sized sublists.
diff --git a/dash.el b/dash.el
index 2a4ffeb..016d5b5 100644
--- a/dash.el
+++ b/dash.el
@@ -448,31 +448,41 @@ FROM or TO may be negative."
   "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)
-  "Returns a new list with the items in LIST grouped into N-sized sublists.
-If there are not enough items to make the last group N-sized,
-those items are discarded."
+(defun ---partition-all-in-steps-reversed (n step list)
+  "Private: Used by -partition-all-in-steps and -partition-in-steps."
+  (when (< step 1)
+    (error "Step must be a positive number, or you're looking at some juicy 
infinite loops."))
   (let ((result nil)
-        (sublist nil)
         (len 0))
     (while list
-      (!cons (car list) sublist)
-      (setq len (1+ len))
-      (when (= len n)
-        (!cons (nreverse sublist) result)
-        (setq sublist nil)
-        (setq len 0))
-      (!cdr list))
+      (!cons (-take n list) result)
+      (setq list (-drop step list)))
+    result))
+
+(defun -partition-all-in-steps (n step list)
+  "Returns a new list with the items in LIST grouped into N-sized sublists at 
offsets STEP apart.
+The last groups may contain less than N items."
+  (nreverse (---partition-all-in-steps-reversed n step list)))
+
+(defun -partition-in-steps (n step list)
+  "Returns a new list with the items in LIST grouped into N-sized sublists at 
offsets STEP apart.
+If there are not enough items to make the last group N-sized,
+those items are discarded."
+  (let ((result (---partition-all-in-steps-reversed n step list)))
+    (while (< (length (car result)) n)
+      (!cdr result))
     (nreverse result)))
 
 (defun -partition-all (n list)
   "Returns a new list with the items in LIST grouped into N-sized sublists.
 The last group may contain less than N items."
-  (let (result)
-    (while list
-      (!cons (-take n list) result)
-      (setq list (-drop n list)))
-    (nreverse result)))
+  (-partition-all-in-steps n n list))
+
+(defun -partition (n list)
+  "Returns a new list with the items in LIST grouped into N-sized sublists.
+If there are not enough items to make the last group N-sized,
+those items are discarded."
+  (-partition-in-steps n n list))
 
 (defmacro --partition-by (form list)
   "Anaphoric form of `-partition-by'."
diff --git a/dev/examples.el b/dev/examples.el
index 67b0dc7..c6f99df 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -169,6 +169,16 @@
   (-partition 2 '(1 2 3 4 5 6 7)) => '((1 2) (3 4) (5 6))
   (-partition 3 '(1 2 3 4 5 6 7)) => '((1 2 3) (4 5 6)))
 
+(defexamples -partition-all-in-steps
+  (-partition-all-in-steps 2 1 '(1 2 3 4)) => '((1 2) (2 3) (3 4) (4))
+  (-partition-all-in-steps 3 2 '(1 2 3 4)) => '((1 2 3) (3 4))
+  (-partition-all-in-steps 3 2 '(1 2 3 4 5)) => '((1 2 3) (3 4 5) (5)))
+
+(defexamples -partition-in-steps
+  (-partition-in-steps 2 1 '(1 2 3 4)) => '((1 2) (2 3) (3 4))
+  (-partition-in-steps 3 2 '(1 2 3 4)) => '((1 2 3))
+  (-partition-in-steps 3 2 '(1 2 3 4 5)) => '((1 2 3) (3 4 5)))
+
 (defexamples -partition-all
   (-partition-all 2 '(1 2 3 4 5 6)) => '((1 2) (3 4) (5 6))
   (-partition-all 2 '(1 2 3 4 5 6 7)) => '((1 2) (3 4) (5 6) (7))



reply via email to

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