[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 3c546ab 178/439: Add -partition-in-steps & -partit
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 3c546ab 178/439: Add -partition-in-steps & -partition-all-in-steps |
Date: |
Tue, 04 Aug 2015 20:27:40 +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))
- [elpa] externals/dash d5c124d 170/439: Update @shosti's list of contributions., (continued)
- [elpa] externals/dash d5c124d 170/439: Update @shosti's list of contributions., Phillip Lord, 2015/08/04
- [elpa] externals/dash 1754ae2 171/439: Typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79e3b92 172/439: Fix -when-let and -if-let docstrings, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0ff070b 169/439: Merge pull request #28 from shosti/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 73ac975 174/439: Merge pull request #29 from shosti/when-let-multi, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8db9e1 176/439: Avoid unnecessary copying for -insert-at, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6d328e4 175/439: Release 1.2.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f9fca3 177/439: Merge pull request #30 from shosti/insert-at-opt, Phillip Lord, 2015/08/04
- [elpa] externals/dash 336e5a5 181/439: Release 1.3.1, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6437adc 179/439: Release 1.3.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3c546ab 178/439: Add -partition-in-steps & -partition-all-in-steps,
Phillip Lord <=
- [elpa] externals/dash e870be8 173/439: Add -when-let* and -if-let*, Phillip Lord, 2015/08/04
- [elpa] externals/dash a3faf7c 183/439: Release 1.3.2, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5614753 182/439: Fix bug with -partition-all-in-steps, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4c8c410 180/439: Add new functions to syntax highlighting, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49de164 184/439: Declare debug-info for some macros, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1fc6679 185/439: Add -map-indexed to syntax highlighting, Phillip Lord, 2015/08/04
- [elpa] externals/dash 50659cc 189/439: Release 1.4.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash ff3d7bd 190/439: Add -sum function., Phillip Lord, 2015/08/04
- [elpa] externals/dash 12291f3 191/439: Add -product function., Phillip Lord, 2015/08/04
- [elpa] externals/dash de67c4b 188/439: Add `-sort`, Phillip Lord, 2015/08/04