[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 467dfa7 133/426: Add -map-indexed
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 467dfa7 133/426: Add -map-indexed |
Date: |
Tue, 04 Aug 2015 19:37:12 +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 3044e83 129/426: Merge pull request #8 from tkf/group-by, (continued)
- [elpa] externals/dash 3044e83 129/426: Merge pull request #8 from tkf/group-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash 85bd4e0 131/426: Add @tali713's -applify, Phillip Lord, 2015/08/04
- [elpa] externals/dash e9de223 116/426: Rename -replace-where to -map-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash 002e4cd 132/426: Add tali713 to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e4ee06 135/426: 1.0.3, Phillip Lord, 2015/08/04
- [elpa] externals/dash e4521af 136/426: Add -repeat, Phillip Lord, 2015/08/04
- [elpa] externals/dash 69e054e 124/426: Switch around order of Contribute and Contributors parts in readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 78390f6 139/426: Merge pull request #12 from vemv/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1284bc6 134/426: Move .el files out of root., Phillip Lord, 2015/08/04
- [elpa] externals/dash da8dc62 138/426: Preserve newline at new-keywords, Phillip Lord, 2015/08/04
- [elpa] externals/dash 467dfa7 133/426: Add -map-indexed,
Phillip Lord <=
- [elpa] externals/dash a196ac1 137/426: Fix -repeat, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9c5fbc5 143/426: nil is the default binding in let forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8fe15ed 141/426: Group -repeat with -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 98a82ca 142/426: DRY up -repeat with --dotimes, Phillip Lord, 2015/08/04
- [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