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

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

[elpa] externals/dash 3f604cd 151/439: Add -count


From: Phillip Lord
Subject: [elpa] externals/dash 3f604cd 151/439: Add -count
Date: Tue, 04 Aug 2015 20:27:21 +0000

branch: externals/dash
commit 3f604cde37b912a3bed8fccd306e160434dae4e4
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>

    Add -count
---
 README.md       |   10 ++++++++++
 dash.el         |   11 +++++++++++
 dev/examples.el |    4 ++++
 3 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index fcedcbc..a710f64 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,7 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-concat](#-concat-rest-lists) `(&rest lists)`
 * [-mapcat](#-mapcat-fn-list) `(fn list)`
 * [-cons*](#-cons-rest-args) `(&rest args)`
+* [-count](#-count-pred-list) `(pred list)`
 * [-any?](#-any-pred-list) `(pred list)`
 * [-all?](#-all-pred-list) `(pred list)`
 * [-none?](#-none-pred-list) `(pred list)`
@@ -233,6 +234,15 @@ a dotted list.
 (-cons* 1) ;; => 1
 ```
 
+### -count `(pred list)`
+
+Counts the number of items in `list` where (`pred` item) is non-nil.
+
+```cl
+(-count 'even? '(1 2 3 4 5)) ;; => 2
+(--count (< it 4) '(1 2 3 4)) ;; => 3
+```
+
 ### -any? `(pred list)`
 
 Returns t if (`pred` x) is non-nil for any x in `list`, else nil.
diff --git a/dash.el b/dash.el
index 6ac7310..4c40dc8 100644
--- a/dash.el
+++ b/dash.el
@@ -250,6 +250,17 @@ a dotted list."
 To get the first item in the list no questions asked, use `car'."
   (--first (funcall pred it) list))
 
+(defmacro --count (pred list)
+  "Anaphoric form of `-count'."
+  (let ((r (make-symbol "result")))
+    `(let ((,r 0))
+       (--each ,list (when ,pred (setq ,r (1+ ,r))))
+       ,r)))
+
+(defun -count (pred list)
+  "Counts the number of items in LIST where (PRED item) is non-nil."
+  (--count (funcall pred it) list))
+
 (defun ---truthy? (val)
   (not (null val)))
 
diff --git a/dev/examples.el b/dev/examples.el
index f18a77a..9dbb09a 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -77,6 +77,10 @@
   (-cons* 1 2 3) => '(1 2 . 3)
   (-cons* 1) => 1)
 
+(defexamples -count
+  (-count 'even? '(1 2 3 4 5)) => 2
+  (--count (< it 4) '(1 2 3 4)) => 3)
+
 (defexamples -any?
   (-any? 'even? '(1 2 3)) => t
   (-any? 'even? '(1 3 5)) => nil



reply via email to

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