emacs-elpa-diffs
[Top][All Lists]
Advanced

[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))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]