[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash ed47814 105/439: Add -dotimes
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash ed47814 105/439: Add -dotimes |
Date: |
Tue, 04 Aug 2015 20:26:56 +0000 |
branch: externals/dash
commit ed4781435be1222424c6a364ac12a3fa8813bf9b
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add -dotimes
---
README.md | 10 ++++++++++
dash.el | 11 +++++++++++
examples.el | 4 ++++
3 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 15725a3..8dfb7e4 100644
--- a/README.md
+++ b/README.md
@@ -26,6 +26,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-none?](#-none-fn-list) `(fn list)`
* [-each](#-each-list-fn) `(list fn)`
* [-each-while](#-each-while-list-pred-fn) `(list pred fn)`
+* [-dotimes](#-dotimes-num-fn) `(num fn)`
* [-take](#-take-n-list) `(n list)`
* [-drop](#-drop-n-list) `(n list)`
* [-take-while](#-take-while-fn-list) `(fn list)`
@@ -238,6 +239,15 @@ Returns nil, used for side-effects only.
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s) ;; => '(2 1)
```
+### -dotimes `(num fn)`
+
+Repeatedly calls `fn` (presumably for side-effects) passing in integers from 0
through n-1.
+
+```cl
+(let (s) (-dotimes 3 (lambda (n) (!cons n s))) s) ;; => '(2 1 0)
+(let (s) (--dotimes 5 (!cons it s)) s) ;; => '(4 3 2 1 0)
+```
+
### -take `(n list)`
Returns a new list of the first `n` items in `list`, or all items if there are
fewer than `n`.
diff --git a/dash.el b/dash.el
index 7a81131..18ebb54 100644
--- a/dash.el
+++ b/dash.el
@@ -64,6 +64,17 @@
Returns nil, used for side-effects only."
(--each-while list (funcall pred it) (funcall fn it)))
+(defmacro --dotimes (num &rest body)
+ "Repeatedly executes BODY (presumably for side-effects) with `it` bound to
integers from 0 through n-1."
+ `(let ((it 0))
+ (while (< it ,num)
+ ,@body
+ (setq it (1+ it)))))
+
+(defun -dotimes (num fn)
+ "Repeatedly calls FN (presumably for side-effects) passing in integers from
0 through n-1."
+ (--dotimes num (funcall fn it)))
+
(defun -map (fn list)
"Returns a new list consisting of the result of applying FN to the items in
LIST."
(mapcar fn list))
diff --git a/examples.el b/examples.el
index a0aca3a..e5eb0f5 100644
--- a/examples.el
+++ b/examples.el
@@ -87,6 +87,10 @@
(let (s) (-each-while '(2 4 5 6) 'even? (lambda (item) (!cons item s))) s)
=> '(4 2)
(let (s) (--each-while '(1 2 3 4) (< it 3) (!cons it s)) s) => '(2 1))
+(defexamples -dotimes
+ (let (s) (-dotimes 3 (lambda (n) (!cons n s))) s) => '(2 1 0)
+ (let (s) (--dotimes 5 (!cons it s)) s) => '(4 3 2 1 0))
+
(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 f1c8453 083/439: Rename to dash., (continued)
- [elpa] externals/dash f1c8453 083/439: Rename to dash., Phillip Lord, 2015/08/04
- [elpa] externals/dash eacb6f2 092/439: Docs: Better generation of github urls., Phillip Lord, 2015/08/04
- [elpa] externals/dash 12ed9f6 100/439: Add destructive operations !cons og !cdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79b56cc 101/439: Use --each, !cons and !cdr to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash 81a3247 104/439: Let -each and -each-while take multiple forms., Phillip Lord, 2015/08/04
- [elpa] externals/dash ba60707 103/439: Use --each-while to simplify implementations., Phillip Lord, 2015/08/04
- [elpa] externals/dash eb50d17 106/439: Add Melpa back in., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4fedd2e 099/439: Add -partition and -partition-all, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38eed45 095/439: Add rainbow-dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49512b9 107/439: Simplify -take and -drop with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed47814 105/439: Add -dotimes,
Phillip Lord <=
- [elpa] externals/dash a08fc14 102/439: Add -each-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash c56925a 111/439: Add font-lock for new functions and the `it` token., Phillip Lord, 2015/08/04
- [elpa] externals/dash a336d78 112/439: Add -only-some?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 03370ea 108/439: Simplify examples-to-docs and examples-to-tests with dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash ba80875 109/439: Nice indentation for --each, --each-while and --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9a3dfdd 113/439: Name parameter PRED when function expects predicate., Phillip Lord, 2015/08/04
- [elpa] externals/dash a081c72 114/439: Fix tests., Phillip Lord, 2015/08/04
- [elpa] externals/dash 9faa422 115/439: Add -partition-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51ddc13 110/439: Nice indentation for threading macros., Phillip Lord, 2015/08/04
- [elpa] externals/dash 2c5b86e 117/439: Add -join as alias to -distinct., Phillip Lord, 2015/08/04