[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 4fedd2e 099/426: Add -partition and -partition-all
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 4fedd2e 099/426: Add -partition and -partition-all |
Date: |
Tue, 04 Aug 2015 19:36:58 +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")
- [elpa] externals/dash f8d74ff 091/426: Docs: update example text to match example code., (continued)
- [elpa] externals/dash f8d74ff 091/426: Docs: update example text to match example code., Phillip Lord, 2015/08/04
- [elpa] externals/dash eacb6f2 092/426: Docs: Better generation of github urls., Phillip Lord, 2015/08/04
- [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 <=
- [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, 2015/08/04
- [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