[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash b9b6330 079/426: Add !take
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash b9b6330 079/426: Add !take |
Date: |
Tue, 04 Aug 2015 19:36:50 +0000 |
branch: externals/dash
commit b9b63302162c47fb711f5845d8f48a1ec2d321d4
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add !take
---
README.md | 10 ++++++++++
bang.el | 9 +++++++++
examples.el | 4 ++++
3 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 335ea17..98f1ea3 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,7 @@ Or you can just dump `bang.el` in your load path somewhere.
* [!keep](#keep-fn-list) `(fn list)`
* [!concat](#concat-rest-lists) `(&rest lists)`
* [!mapcat](#mapcat-fn-list) `(fn list)`
+* [!take](#take-n-list) `(n list)`
* [!take-while](#take-while-fn-list) `(fn list)`
* [!drop-while](#drop-while-fn-list) `(fn list)`
* [!split-with](#split-with-fn-list) `(fn list)`
@@ -165,6 +166,15 @@ Thus function `fn` should return a collection.
(!!mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3)
```
+### !take `(n list)`
+
+Returns a new list of the first `n` items in `list`, or all items if there are
fewer than `n`.
+
+```cl
+(!take 3 '(1 2 3 4 5)) ;; => '(1 2 3)
+(!take 17 '(1 2 3 4 5)) ;; => '(1 2 3 4 5)
+```
+
### !take-while `(fn list)`
Returns a new list of successive items from `list` while (`fn` item) returns a
non-nil value.
diff --git a/bang.el b/bang.el
index 2b9e203..4320a74 100644
--- a/bang.el
+++ b/bang.el
@@ -144,6 +144,15 @@ the supplied LISTS."
Thus function FN should return a collection."
(!!mapcat (funcall fn it) 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)
+ (while (and list (> n 0))
+ (setq result (cons (car list) result))
+ (setq list (cdr list))
+ (setq n (1- n)))
+ (nreverse result)))
+
(defmacro !!take-while (form list)
"Anaphoric form of `!take-while'."
(let ((l (make-symbol "list"))
diff --git a/examples.el b/examples.el
index 8d4283d..c08d46b 100644
--- a/examples.el
+++ b/examples.el
@@ -58,6 +58,10 @@
(!mapcat (lambda (item) (list 0 item)) '(1 2 3)) => '(0 1 0 2 0 3)
(!!mapcat (list 0 it) '(1 2 3)) => '(0 1 0 2 0 3))
+(defexamples !take
+ (!take 3 '(1 2 3 4 5)) => '(1 2 3)
+ (!take 17 '(1 2 3 4 5)) => '(1 2 3 4 5))
+
(defexamples !take-while
(!take-while 'even? '(1 2 3 4)) => '()
(!take-while 'even? '(2 4 5 6)) => '(2 4)
- [elpa] externals/dash 82a12a2 076/426: Add !drop-while, (continued)
- [elpa] externals/dash 82a12a2 076/426: Add !drop-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash fea1aa3 086/426: Add -interleave, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38cc9e7 088/426: Simplify -interleave with -none?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ff9fd0 085/426: Move functions that are not strictly list related last., Phillip Lord, 2015/08/04
- [elpa] externals/dash 0c55a4b 090/426: 1.0.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4b5e24b 087/426: Docs: move -any? -all? -none? and -each up., Phillip Lord, 2015/08/04
- [elpa] externals/dash f8d74ff 091/426: Docs: update example text to match example code., Phillip Lord, 2015/08/04
- [elpa] externals/dash eacb6f2 092/426: Docs: Better generation of github urls., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8c75026 080/426: Add !drop, Phillip Lord, 2015/08/04
- [elpa] externals/dash a2941cd 097/426: Typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash b9b6330 079/426: Add !take,
Phillip Lord <=
- [elpa] externals/dash 6931d8a 094/426: Remove Melpa from installation while waiting for it to be included., Phillip Lord, 2015/08/04
- [elpa] externals/dash 0bd26ac 096/426: Add aliases for -p instead of ?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38eed45 095/426: Add rainbow-dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash dc6f46c 098/426: Simplify docs-generation using dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash b0da7b2 093/426: Declare macros before using them., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4fedd2e 099/426: Add -partition and -partition-all, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79b56cc 101/426: Use --each, !cons and !cdr to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash a08fc14 102/426: Add -each-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49512b9 107/426: Simplify -take and -drop with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed47814 105/426: Add -dotimes, Phillip Lord, 2015/08/04