[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")
- [elpa] externals/dash 0bd26ac 096/439: Add aliases for -p instead of ?, (continued)
- [elpa] externals/dash 0bd26ac 096/439: Add aliases for -p instead of ?, Phillip Lord, 2015/08/04
- [elpa] externals/dash a2941cd 097/439: Typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash dc6f46c 098/439: Simplify docs-generation using dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash f1c8453 083/439: Rename to dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash eacb6f2 092/439: Docs: Better generation of github urls., Phillip Lord, 2015/08/04
- [elpa] externals/dash 12ed9f6 100/439: Add destructive operations !cons og !cdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79b56cc 101/439: Use --each, !cons and !cdr to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash 81a3247 104/439: Let -each and -each-while take multiple forms., Phillip Lord, 2015/08/04
- [elpa] externals/dash ba60707 103/439: Use --each-while to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash eb50d17 106/439: Add Melpa back in., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4fedd2e 099/439: Add -partition and -partition-all,
Phillip Lord <=
- [elpa] externals/dash 38eed45 095/439: Add rainbow-dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49512b9 107/439: Simplify -take and -drop with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed47814 105/439: Add -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash a08fc14 102/439: Add -each-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash c56925a 111/439: Add font-lock for new functions and the `it` token., Phillip Lord, 2015/08/04
- [elpa] externals/dash a336d78 112/439: Add -only-some?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 03370ea 108/439: Simplify examples-to-docs and examples-to-tests with dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash ba80875 109/439: Nice indentation for --each, --each-while and --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9a3dfdd 113/439: Name parameter PRED when function expects predicate., Phillip Lord, 2015/08/04
- [elpa] externals/dash a081c72 114/439: Fix tests., Phillip Lord, 2015/08/04