[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 467dfa7 133/439: Add -map-indexed
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 467dfa7 133/439: Add -map-indexed |
Date: |
Tue, 04 Aug 2015 20:27:11 +0000 |
branch: externals/dash
commit 467dfa777dc53e0d1106f4d25ac802eaacae2744
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add -map-indexed
---
README.md | 12 ++++++++++++
dash.el | 19 ++++++++++++++++++-
examples.el | 4 ++++
3 files changed, 34 insertions(+), 1 deletions(-)
diff --git a/README.md b/README.md
index ba05237..b2e84a3 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-remove](#-remove-pred-list) `(pred list)`
* [-keep](#-keep-fn-list) `(fn list)`
* [-map-when](#-map-when-pred-rep-list) `(pred rep list)`
+* [-map-indexed](#-map-indexed-fn-list) `(fn list)`
* [-flatten](#-flatten-l) `(l)`
* [-concat](#-concat-rest-lists) `(&rest lists)`
* [-mapcat](#-mapcat-fn-list) `(fn list)`
@@ -173,6 +174,17 @@ through the `rep` function.
(--map-when (= it 2) 17 '(1 2 3 4)) ;; => '(1 17 3 4)
```
+### -map-indexed `(fn list)`
+
+Returns a new list consisting of the result of (`fn` index item) for each item
in `list`.
+
+In the anaphoric form `--map-indexed`, the index is exposed as `it-index`.
+
+```cl
+(-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) ;; => '(1 1 1 1)
+(--map-indexed (- it it-index) '(1 2 3 4)) ;; => '(1 1 1 1)
+```
+
### -flatten `(l)`
Takes a nested list `l` and returns its contents as a single, flat list.
diff --git a/dash.el b/dash.el
index 30106c2..58c890c 100644
--- a/dash.el
+++ b/dash.el
@@ -38,10 +38,12 @@
(defmacro --each (list &rest body)
"Anaphoric form of `-each'."
(let ((l (make-symbol "list")))
- `(let ((,l ,list))
+ `(let ((,l ,list)
+ (it-index 0))
(while ,l
(let ((it (car ,l)))
,@body)
+ (setq it-index (1+ it-index))
(!cdr ,l)))))
(put '--each 'lisp-indent-function 1)
@@ -174,6 +176,20 @@ Alias: `-reject'"
(--each ,list (!cons (if ,pred ,rep it) ,r))
(nreverse ,r))))
+(defmacro --map-indexed (form list)
+ "Anaphoric form of `-map-indexed'."
+ (let ((r (make-symbol "result")))
+ `(let (,r)
+ (--each ,list
+ (!cons ,form ,r))
+ (nreverse ,r))))
+
+(defun -map-indexed (fn list)
+ "Returns a new list consisting of the result of (FN index item) for each
item in LIST.
+
+In the anaphoric form `--map-indexed', the index is exposed as `it-index`."
+ (--map-indexed (funcall fn it-index it) list))
+
(defun -map-when (pred rep list)
"Returns a new list where the elements in LIST that does not match the PRED
function
are unchanged, and where the elements in LIST that do match the PRED function
are mapped
@@ -658,6 +674,7 @@ or with `-compare-fn' if that's non-nil."
))
(special-variables '(
"it"
+ "it-index"
"acc"
)))
(font-lock-add-keywords 'emacs-lisp-mode `((,(concat "\\<" (regexp-opt
special-variables 'paren) "\\>")
diff --git a/examples.el b/examples.el
index 05738ee..e9686ac 100644
--- a/examples.el
+++ b/examples.el
@@ -53,6 +53,10 @@
(--map-when (= it 2) 17 '(1 2 3 4)) => '(1 17 3 4)
(-map-when (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4))
+(defexamples -map-indexed
+ (-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) => '(1 1 1 1)
+ (--map-indexed (- it it-index) '(1 2 3 4)) => '(1 1 1 1))
+
(defexamples -flatten
(-flatten '((1))) => '(1)
(-flatten '((1 (2 3) (((4 (5))))))) => '(1 2 3 4 5))
- [elpa] externals/dash cca9b1b 121/439: Also run tests on Emacs24, (continued)
- [elpa] externals/dash cca9b1b 121/439: Also run tests on Emacs24, Phillip Lord, 2015/08/04
- [elpa] externals/dash d857f73 118/439: Merge pull request #3 from rejeep/join-alias, Phillip Lord, 2015/08/04
- [elpa] externals/dash c025efb 122/439: Add -separate (thanks @Fuco1), Phillip Lord, 2015/08/04
- [elpa] externals/dash 69e054e 124/439: Switch around order of Contribute and Contributors parts in readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f91ec8 126/439: 1.0.2, Phillip Lord, 2015/08/04
- [elpa] externals/dash f034c16 119/439: Add -union (thanks to @Fuco1), Phillip Lord, 2015/08/04
- [elpa] externals/dash 5e72ff9 120/439: Include numbers in github ids., Phillip Lord, 2015/08/04
- [elpa] externals/dash a23aa4b 128/439: Add examples for -group-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash c669282 123/439: Add @Fuco1 to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash 7f7ac33 130/439: Add Takafumi Arakaki to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash 467dfa7 133/439: Add -map-indexed,
Phillip Lord <=
- [elpa] externals/dash 85bd4e0 131/439: Add @tali713's -applify, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e4ee06 135/439: 1.0.3, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1284bc6 134/439: Move .el files out of root., Phillip Lord, 2015/08/04
- [elpa] externals/dash 1120417 127/439: Add -group-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1c4e5d7 125/439: Remove eval from --reduce, it is evil., Phillip Lord, 2015/08/04
- [elpa] externals/dash 3044e83 129/439: Merge pull request #8 from tkf/group-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash 78390f6 139/439: Merge pull request #12 from vemv/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0731333 140/439: Add @vemv to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash 002e4cd 132/439: Add tali713 to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash 98a82ca 142/439: DRY up -repeat with --dotimes, Phillip Lord, 2015/08/04