[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash a4b70f9 154/426: [magnars/dash.el#18] implementing
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash a4b70f9 154/426: [magnars/dash.el#18] implementing -slice |
Date: |
Tue, 04 Aug 2015 19:37:22 +0000 |
branch: externals/dash
commit a4b70f96c924986ff5fa595f180c1f0fa5bb2ac7
Author: Wilfred Hughes <address@hidden>
Commit: Wilfred Hughes <address@hidden>
[magnars/dash.el#18] implementing -slice
---
dash.el | 22 ++++++++++++++++++++++
dev/examples.el | 5 +++++
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/dash.el b/dash.el
index a924424..5b2b3b3 100644
--- a/dash.el
+++ b/dash.el
@@ -331,6 +331,28 @@ 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)
+ "Return copy of LIST, starting from index FROM to index TO.
+FROM or TO may be negative."
+ (let ((length (length list))
+ (new-list nil)
+ (index 0))
+ ;; to defaults to the end of the list
+ (setq to (or to length))
+ ;; handle negative indices
+ (when (< from 0)
+ (setq from (mod from length)))
+ (when (< to 0)
+ (setq to (mod to length)))
+
+ ;; iterate through the list, keeping the elements we want
+ (while (< index to)
+ (when (>= index from)
+ (!cons (car list) new-list))
+ (!cdr list)
+ (setq index (1+ index)))
+ (nreverse new-list)))
+
(defun -take (n list)
"Returns a new list of the first N items in LIST, or all items if there are
fewer than N."
(let (result)
diff --git a/dev/examples.el b/dev/examples.el
index 2fb37d9..40a1ed3 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -122,6 +122,11 @@
(-repeat 0 :a) => nil
(-repeat -1 :a) => nil)
+(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))
+
(defexamples -take
(-take 3 '(1 2 3 4 5)) => '(1 2 3)
(-take 17 '(1 2 3 4 5)) => '(1 2 3 4 5))
- [elpa] externals/dash 50e9f46 144/426: -cons* for making improper lists., (continued)
- [elpa] externals/dash 50e9f46 144/426: -cons* for making improper lists., Phillip Lord, 2015/08/04
- [elpa] externals/dash e083087 145/426: Merge pull request #13 from nicferrier/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5161361 148/426: Clarify use of anaphoric form of -zip-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0731333 140/426: Add @vemv to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash a7ee06e 149/426: Remove extra --separate definition, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3f604cd 151/426: Add -count, Phillip Lord, 2015/08/04
- [elpa] externals/dash fb75a7f 146/426: Add @nicferrier to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash cc33c2f 153/426: Stop integration tests for Emacs 23, Phillip Lord, 2015/08/04
- [elpa] externals/dash adfafcb 152/426: Add -partition-by-header, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0351ca3 156/426: Add Wilfred Hughes to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash a4b70f9 154/426: [magnars/dash.el#18] implementing -slice,
Phillip Lord <=
- [elpa] externals/dash bd942e1 158/426: inhibit dev from being added to the load-path, Phillip Lord, 2015/08/04
- [elpa] externals/dash c218e9f 150/426: -split-at/with now makes just one pass over list, Phillip Lord, 2015/08/04
- [elpa] externals/dash d7174ed 160/426: Handle cons-cells properly in -flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash a559a80 161/426: Added -last, Phillip Lord, 2015/08/04
- [elpa] externals/dash f7a828f 147/426: Add -zip and -zip-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash dac28b0 155/426: Merge pull request #19 from Wilfred/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash a40fc00 162/426: Merge pull request #23 from Fuco1/last, Phillip Lord, 2015/08/04
- [elpa] externals/dash a1b04d8 163/426: Less implementation specific docs for -mapcat, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6ffdc93 157/426: Release 1.1.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1396102 165/426: Add -when-let and -if-let macros, Phillip Lord, 2015/08/04