[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 7185db8 311/439: [Issue #83] Add `step` to `-slice
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 7185db8 311/439: [Issue #83] Add `step` to `-slice` |
Date: |
Tue, 04 Aug 2015 20:29:33 +0000 |
branch: externals/dash
commit 7185db8b78f90881cbca2d5eb97eaa66b2cc077a
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
[Issue #83] Add `step` to `-slice`
---
README.md | 13 +++++++++----
dash.el | 13 ++++++++++---
dev/examples.el | 13 +++++++++++--
3 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/README.md b/README.md
index 34443e2..ac54f94 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ Include this in your emacs settings to get syntax
highlighting:
* [-flatten-n](#-flatten-n-num-list) `(num list)`
* [-concat](#-concat-rest-lists) `(&rest lists)`
* [-mapcat](#-mapcat-fn-list) `(fn list)`
-* [-slice](#-slice-list-from-optional-to) `(list from &optional to)`
+* [-slice](#-slice-list-from-optional-to-step) `(list from &optional to step)`
* [-take](#-take-n-list) `(n list)`
* [-drop](#-drop-n-list) `(n list)`
* [-take-while](#-take-while-pred-list) `(pred list)`
@@ -335,15 +335,20 @@ Thus function `fn` should return a list.
(--mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3)
```
-#### -slice `(list from &optional to)`
+#### -slice `(list from &optional to step)`
Return copy of `list`, starting from index `from` to index `to`.
-`from` or `to` may be negative.
+
+`from` or `to` may be negative. These values are then interpreted
+modulo the length of the list.
+
+If `step` is a number, only each STEPth item in the resulting
+section is returned. Defaults to 1.
```cl
(-slice '(1 2 3 4 5) 1) ;; => '(2 3 4 5)
(-slice '(1 2 3 4 5) 0 3) ;; => '(1 2 3)
-(-slice '(1 2 3 4 5) 1 -1) ;; => '(2 3 4)
+(-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) ;; => '(2 4 6 8)
```
#### -take `(n list)`
diff --git a/dash.el b/dash.el
index 7c1a772..240b93d 100644
--- a/dash.el
+++ b/dash.el
@@ -432,13 +432,19 @@ Returns `nil` both if all items match the predicate, and
if none of the items ma
(defalias '-only-some-p '-only-some?)
(defalias '--only-some-p '--only-some?)
-(defun -slice (list from &optional to)
+(defun -slice (list from &optional to step)
"Return copy of LIST, starting from index FROM to index TO.
-FROM or TO may be negative."
+
+FROM or TO may be negative. These values are then interpreted
+modulo the length of the list.
+
+If STEP is a number, only each STEPth item in the resulting
+section is returned. Defaults to 1."
(let ((length (length list))
(new-list nil))
;; to defaults to the end of the list
(setq to (or to length))
+ (setq step (or step 1))
;; handle negative indices
(when (< from 0)
(setq from (mod from length)))
@@ -447,7 +453,8 @@ FROM or TO may be negative."
;; iterate through the list, keeping the elements we want
(--each-while list (< it-index to)
- (when (>= it-index from)
+ (when (and (>= it-index from)
+ (= (mod (- from it-index) step) 0))
(push it new-list)))
(nreverse new-list)))
diff --git a/dev/examples.el b/dev/examples.el
index 28fceb6..aabd343 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -70,10 +70,19 @@
(defexamples -slice
(-slice '(1 2 3 4 5) 1) => '(2 3 4 5)
(-slice '(1 2 3 4 5) 0 3) => '(1 2 3)
- (-slice '(1 2 3 4 5) 1 -1) => '(2 3 4)
+ (-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2) => '(2 4 6 8)
(-slice '(1 2 3 4 5) 0 10) => '(1 2 3 4 5) ;; "to > length" should not
fill in nils!
(-slice '(1 2 3 4 5) -3) => '(3 4 5)
- (-slice '(1 2 3 4 5) -3 -1) => '(3 4))
+ (-slice '(1 2 3 4 5) -3 -1) => '(3 4)
+ (-slice '(1 2 3 4 5 6) 0 nil 1) => '(1 2 3 4 5 6)
+ (-slice '(1 2 3 4 5 6) 0 nil 2) => '(1 3 5)
+ (-slice '(1 2 3 4 5 6) 0 nil 3) => '(1 4)
+ (-slice '(1 2 3 4 5 6) 0 nil 10) => '(1)
+ (-slice '(1 2 3 4 5 6) 1 4 2) => '(2 4)
+ (-slice '(1 2 3 4 5 6) 2 6 3) => '(3 6)
+ (-slice '(1 2 3 4 5 6) 2 -1 2) => '(3 5)
+ (-slice '(1 2 3 4 5 6) -4 -1 2) => '(3 5)
+ (-slice '(1 2 3 4 5 6) 1 2 10) => '(2))
(defexamples -take
(-take 3 '(1 2 3 4 5)) => '(1 2 3)
- [elpa] externals/dash c952a54 300/439: Merge pull request #90 from tmalsburg/last-item-doc, (continued)
- [elpa] externals/dash c952a54 300/439: Merge pull request #90 from tmalsburg/last-item-doc, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2dfd748 298/439: Merge pull request #87 from steventlamb/topic/make_zip_n_ary, Phillip Lord, 2015/08/04
- [elpa] externals/dash bd85b7c 302/439: Make -zip support infinite (circular) lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0dd4cc 301/439: Add -annotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash f780322 305/439: Add `-flatten-n`, Phillip Lord, 2015/08/04
- [elpa] externals/dash fb51f8f 304/439: Merge pull request #88 from steventlamb/topic/zip_cycle_pad, Phillip Lord, 2015/08/04
- [elpa] externals/dash 00549e4 307/439: Add missing fontification keywords, Phillip Lord, 2015/08/04
- [elpa] externals/dash cd137e0 310/439: `-slice` should not fill the returned list with nils if to > length, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6b64ea7 303/439: Add functions for flexibly zipping uneven lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash 63ec298 308/439: Merge pull request #92 from Fuco1/outer-product, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7185db8 311/439: [Issue #83] Add `step` to `-slice`,
Phillip Lord <=
- [elpa] externals/dash a3021eb 313/439: Merge pull request #93 from lunaryorn/patch-1, Phillip Lord, 2015/08/04
- [elpa] externals/dash 624c501 306/439: Add `-table` and `-table-flat`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0384eee 312/439: Improve Travis CI configuration, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3132ae0 314/439: Alias -tail to nthcdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2ecc073 316/439: Reorder the partition examples to follow more logical order, add tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash bbc1d9c 309/439: `--each-while` should also expose `it-index`, Phillip Lord, 2015/08/04
- [elpa] externals/dash d948086 315/439: Add -iteratefn, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7f0fadc 317/439: Add -prodfn, Phillip Lord, 2015/08/04
- [elpa] externals/dash eea928a 320/439: Add -replace, Phillip Lord, 2015/08/04
- [elpa] externals/dash f3b0a55 321/439: Change -predicate-p examples to -predicate? to maintain consistency, Phillip Lord, 2015/08/04