Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #23 from Fuco1/last

Added -last
  • Loading branch information...
commit a40fc004526cc602c38879270739e578e11e33a3 2 parents d7174ed + a559a80
@magnars authored
Showing with 29 additions and 0 deletions.
  1. +12 −0 README.md
  2. +12 −0 dash.el
  3. +5 −0 dev/examples.el
View
12 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`.
View
12 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")))
View
5 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)
Please sign in to comment.
Something went wrong with that request. Please try again.