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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash a559a80 161/439: Added -last


From: Phillip Lord
Subject: [elpa] externals/dash a559a80 161/439: Added -last
Date: Tue, 04 Aug 2015 20:27:28 +0000

branch: externals/dash
commit a559a80d586b67c6f9e35458fbb4541af710c088
Author: Fuco1 <address@hidden>
Commit: Fuco1 <address@hidden>

    Added -last
---
 README.md       |   12 ++++++++++++
 dash.el         |   12 ++++++++++++
 dev/examples.el |    5 +++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index 845ab17..9d47f44 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,7 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-zip-with](#-zip-with-fn-list1-list2) `(fn list1 list2)`
 * [-zip](#-zip-list1-list2) `(list1 list2)`
 * [-first](#-first-pred-list) `(pred list)`
+* [-last](#-last-pred-list) `(pred list)`
 * [-union](#-union-list-list2) `(list list2)`
 * [-difference](#-difference-list-list2) `(list list2)`
 * [-intersection](#-intersection-list-list2) `(list list2)`
@@ -199,6 +200,7 @@ Takes a nested list `l` and returns its contents as a 
single, flat list.
 ```cl
 (-flatten '((1))) ;; => '(1)
 (-flatten '((1 (2 3) (((4 (5))))))) ;; => '(1 2 3 4 5)
+(-flatten '(1 2 (3 . 4))) ;; => '(1 2 (3 . 4))
 ```
 
 ### -concat `(&rest lists)`
@@ -526,6 +528,16 @@ To get the first item in the list no questions asked, use 
`car`.
 (--first (> it 2) '(1 2 3)) ;; => 3
 ```
 
+### -last `(pred list)`
+
+Return the last x in `list` where (`pred` x) is non-nil, else nil.
+
+```cl
+(-last 'even? '(1 2 3 4 5 6 3 3 3)) ;; => 6
+(-last 'even? '(1 3 7 5 9)) ;; => nil
+(--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one")) ;; => 
"short"
+```
+
 ### -union `(list list2)`
 
 Return a new list containing the elements of `list1` and elements of `list2` 
that are not in `list1`.
diff --git a/dash.el b/dash.el
index 6ca2fea..1626476 100644
--- a/dash.el
+++ b/dash.el
@@ -250,6 +250,18 @@ a dotted list."
 To get the first item in the list no questions asked, use `car'."
   (--first (funcall pred it) list))
 
+(defmacro --last (form list)
+  "Anaphoric form of `-last'."
+  (let ((n (make-symbol "needle")))
+    `(let (,n)
+       (--each ,list
+         (when ,form (setq ,n it)))
+       ,n)))
+
+(defun -last (pred list)
+  "Return the last x in LIST where (PRED x) is non-nil, else nil."
+  (--last (funcall pred it) list))
+
 (defmacro --count (pred list)
   "Anaphoric form of `-count'."
   (let ((r (make-symbol "result")))
diff --git a/dev/examples.el b/dev/examples.el
index caa7f5c..b6d9045 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -211,6 +211,11 @@
   (-first 'even? '(1 3 5)) => nil
   (--first (> it 2) '(1 2 3)) => 3)
 
+(defexamples -last
+  (-last 'even? '(1 2 3 4 5 6 3 3 3)) => 6
+  (-last 'even? '(1 3 7 5 9)) => nil
+  (--last (> (length it) 3) '("a" "looong" "word" "and" "short" "one")) => 
"short")
+
 (defexamples -union
   (-union '(1 2 3) '(3 4 5))  => '(1 2 3 4 5)
   (-union '(1 2 3 4) '())  => '(1 2 3 4)



reply via email to

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