[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 7185db8 311/426: [Issue #83] Add `step` to `-slice
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 7185db8 311/426: [Issue #83] Add `step` to `-slice` |
Date: |
Tue, 04 Aug 2015 19:38:31 +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 65a3736 349/426: [-let] Fix improper list non-symbol last argument handler, (continued)
- [elpa] externals/dash 65a3736 349/426: [-let] Fix improper list non-symbol last argument handler, Phillip Lord, 2015/08/04
- [elpa] externals/dash 549bfd3 366/426: Reorder macros to make sure they are declared before being used, Phillip Lord, 2015/08/04
- [elpa] externals/dash db7f651 353/426: [-let] Simplify the conditions in dash--match-cons-1, Phillip Lord, 2015/08/04
- [elpa] externals/dash c6b92ae 324/426: Reorder the examples into more meaningful blocks, Phillip Lord, 2015/08/04
- [elpa] externals/dash 03b98ca 364/426: Make -if-let and -when-let families destructure their arguments, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5e9c1a9 373/426: Add Fredrik Bergroth to the list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8707aaf 367/426: Update docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 63fa64f 368/426: Add links to references in readme, Phillip Lord, 2015/08/04
- [elpa] externals/dash d459b49 374/426: Fix typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9e1a667 360/426: Do not map nodes on conses (be consistent with tree map), Phillip Lord, 2015/08/04
- [elpa] externals/dash 7185db8 311/426: [Issue #83] Add `step` to `-slice`,
Phillip Lord <=
- [elpa] externals/dash 2b69c14 411/426: Add dash.info and dash.texi to repo, Phillip Lord, 2015/08/04
- [elpa] externals/dash c61fc34 417/426: Add -remove-first/last, -map-first/last, -replace-first/last, Phillip Lord, 2015/08/04
- [elpa] externals/dash fb51f8f 304/426: Merge pull request #88 from steventlamb/topic/zip_cycle_pad, Phillip Lord, 2015/08/04
- [elpa] externals/dash d0c6fc0 371/426: Merge pull request #104 from fbergroth/linkify-docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash c08fcba 299/426: Fixed documentation of `-last-item'., Phillip Lord, 2015/08/04
- [elpa] externals/dash f96196a 289/426: Merge pull request #75 from Fuco1/prefix, Phillip Lord, 2015/08/04
- [elpa] externals/dash afe2781 388/426: Merge pull request #109 from bbatsov/threading-indentation, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6b64ea7 303/426: Add functions for flexibly zipping uneven lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash cd137e0 310/426: `-slice` should not fill the returned list with nils if to > length, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5219ac0 422/426: [-let] Eliminate useless re-binding of symbols when sources are immutable, Phillip Lord, 2015/08/04