diff --git a/README.md b/README.md index 4ee7cde..f52d46d 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Include this in your emacs settings to get syntax highlighting: * [-elem-index](#-elem-index-elem-list) `(elem list)` * [-elem-indices](#-elem-indices-elem-list) `(elem list)` * [-find-index](#-find-index-pred-list) `(pred list)` +* [-find-last-index](#-find-last-index-pred-list) `(pred list)` * [-find-indices](#-find-indices-pred-list) `(pred list)` * [-select-by-indices](#-select-by-indices-indices-list) `(indices list)` * [-grade-up](#-grade-up-comparator-list) `(comparator list)` @@ -784,6 +785,18 @@ there is no such element. (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) ;; => 1 ``` +#### -find-last-index `(pred list)` + +Take a predicate `pred` and a `list` and return the index of the +last element in the list satisfying the predicate, or nil if +there is no such element. + +```cl +(-find-last-index 'even? '(2 4 1 6 3 3 5 8)) ;; => 7 +(--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2)) ;; => 5 +(-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz")) ;; => 1 +``` + #### -find-indices `(pred list)` Return the indices of all elements in `list` satisfying the diff --git a/dash.el b/dash.el index 8b58586..2082f11 100644 --- a/dash.el +++ b/dash.el @@ -806,6 +806,16 @@ there is no such element." (declare (debug (sexp form))) `(-find-index (lambda (it) ,form) ,list)) +(defun -find-last-index (pred list) + "Take a predicate PRED and a LIST and return the index of the +last element in the list satisfying the predicate, or nil if +there is no such element." + (-last-item (-find-indices pred list))) + +(defmacro --find-last-index (form list) + "Anaphoric version of `-find-last-index'." + `(-find-last-index (lambda (it) ,form) ,list)) + (defun -select-by-indices (indices list) "Return a list whose elements are elements from LIST selected as `(nth i list)` for all i from INDICES." @@ -1322,6 +1332,8 @@ structure such as plist or alist." "--find-indices" "-find-index" "--find-index" + "-find-last-index" + "--find-last-index" "-select-by-indices" "-grade-up" "-grade-down" diff --git a/dev/examples.el b/dev/examples.el index 88e4f3d..c0ae5bf 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -303,6 +303,11 @@ (--find-index (< 5 it) '(2 4 1 6 3 3 5 8)) => 3 (-find-index (-partial 'string-lessp "baz") '("bar" "foo" "baz")) => 1) + (defexamples -find-last-index + (-find-last-index 'even? '(2 4 1 6 3 3 5 8)) => 7 + (--find-last-index (< 5 it) '(2 7 1 6 3 8 5 2)) => 5 + (-find-last-index (-partial 'string-lessp "baz") '("q" "foo" "baz")) => 1) + (defexamples -find-indices (-find-indices 'even? '(2 4 1 6 3 3 5 8)) => '(0 1 3 7) (--find-indices (< 5 it) '(2 4 1 6 3 3 5 8)) => '(3 7)