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

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

[elpa] externals/dash 4fedd2e 099/439: Add -partition and -partition-all


From: Phillip Lord
Subject: [elpa] externals/dash 4fedd2e 099/439: Add -partition and -partition-all
Date: Tue, 04 Aug 2015 20:26:52 +0000

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

    Add -partition and -partition-all
---
 README.md   |   25 +++++++++++++++++++++++++
 dash.el     |   26 ++++++++++++++++++++++++++
 examples.el |   10 ++++++++++
 3 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index b9bd7dc..419955b 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,8 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-drop-while](#-drop-while-fn-list) `(fn list)`
 * [-split-at](#-split-at-n-list) `(n list)`
 * [-split-with](#-split-with-fn-list) `(fn list)`
+* [-partition](#-partition-n-list) `(n list)`
+* [-partition-all](#-partition-all-n-list) `(n list)`
 * [-interpose](#-interpose-sep-list) `(sep list)`
 * [-interleave](#-interleave-rest-lists) `(&rest lists)`
 * [-replace-where](#-replace-where-pred-rep-list) `(pred rep list)`
@@ -280,6 +282,29 @@ Returns a list of ((-take-while `fn` `list`) (-drop-while 
`fn` `list`))
 (--split-with (< it 4) '(1 2 3 4 3 2 1)) ;; => '((1 2 3) (4 3 2 1))
 ```
 
+### -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.
+
+```cl
+(-partition 2 '(1 2 3 4 5 6)) ;; => '((1 2) (3 4) (5 6))
+(-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))
+```
+
+### -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.
+
+```cl
+(-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))
+(-partition-all 3 '(1 2 3 4 5 6 7)) ;; => '((1 2 3) (4 5 6) (7))
+```
+
 ### -interpose `(sep list)`
 
 Returns a new list of all elements in `list` separated by `sep`.
diff --git a/dash.el b/dash.el
index e6ad781..a115338 100644
--- a/dash.el
+++ b/dash.el
@@ -295,6 +295,32 @@ Alias: `-every?'"
   "Returns a list of ((-take-while FN LIST) (-drop-while FN LIST))"
   (--split-with (funcall fn 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."
+  (let ((result nil)
+        (sublist nil)
+        (len 0))
+    (while list
+      (setq sublist (cons (car list) sublist))
+      (setq len (1+ len))
+      (when (= len n)
+        (setq result (cons (nreverse sublist) result))
+        (setq sublist nil)
+        (setq len 0))
+      (setq list (cdr list)))
+    (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
+      (setq result (cons (-take n list) result))
+      (setq list (-drop n list)))
+    (nreverse result)))
+
 (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 c6b5a5b..1c8211a 100644
--- a/examples.el
+++ b/examples.el
@@ -110,6 +110,16 @@
   (-split-with 'even? '(2 4 5 6)) => '((2 4) (5 6))
   (--split-with (< it 4) '(1 2 3 4 3 2 1)) => '((1 2 3) (4 3 2 1)))
 
+(defexamples -partition
+  (-partition 2 '(1 2 3 4 5 6)) => '((1 2) (3 4) (5 6))
+  (-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
+  (-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))
+  (-partition-all 3 '(1 2 3 4 5 6 7)) => '((1 2 3) (4 5 6) (7)))
+
 (defexamples -interpose
   (-interpose "-" '()) => '()
   (-interpose "-" '("a")) => '("a")



reply via email to

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