[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 3f604cd 151/426: Add -count
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 3f604cd 151/426: Add -count |
Date: |
Tue, 04 Aug 2015 19:37: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
- [elpa] externals/dash 467dfa7 133/426: Add -map-indexed, (continued)
- [elpa] externals/dash 467dfa7 133/426: Add -map-indexed, Phillip Lord, 2015/08/04
- [elpa] externals/dash a196ac1 137/426: Fix -repeat, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9c5fbc5 143/426: nil is the default binding in let forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8fe15ed 141/426: Group -repeat with -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 98a82ca 142/426: DRY up -repeat with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 50e9f46 144/426: -cons* for making improper lists., Phillip Lord, 2015/08/04
- [elpa] externals/dash e083087 145/426: Merge pull request #13 from nicferrier/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5161361 148/426: Clarify use of anaphoric form of -zip-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0731333 140/426: Add @vemv to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash a7ee06e 149/426: Remove extra --separate definition, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3f604cd 151/426: Add -count,
Phillip Lord <=
- [elpa] externals/dash fb75a7f 146/426: Add @nicferrier to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash cc33c2f 153/426: Stop integration tests for Emacs 23, Phillip Lord, 2015/08/04
- [elpa] externals/dash adfafcb 152/426: Add -partition-by-header, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0351ca3 156/426: Add Wilfred Hughes to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash a4b70f9 154/426: [magnars/dash.el#18] implementing -slice, Phillip Lord, 2015/08/04
- [elpa] externals/dash bd942e1 158/426: inhibit dev from being added to the load-path, Phillip Lord, 2015/08/04
- [elpa] externals/dash c218e9f 150/426: -split-at/with now makes just one pass over list, Phillip Lord, 2015/08/04
- [elpa] externals/dash d7174ed 160/426: Handle cons-cells properly in -flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash a559a80 161/426: Added -last, Phillip Lord, 2015/08/04
- [elpa] externals/dash f7a828f 147/426: Add -zip and -zip-with, Phillip Lord, 2015/08/04