[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash f59b480 055/426: Add !some and !every?
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash f59b480 055/426: Add !some and !every? |
Date: |
Tue, 04 Aug 2015 19:36:40 +0000 |
branch: externals/dash
commit f59b480a49e5cb575544fe6d4a76c4dcb6b77f0e
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add !some and !every?
---
README.md | 22 ++++++++++++++++++++++
bang.el | 28 ++++++++++++++++++++++++++++
examples.el | 10 ++++++++++
3 files changed, 60 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 2daa608..f987e4f 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,8 @@ This is so much a work in progress that you should definitely
not be using it ye
* [!intersection](#intersection-list-list2) `(list list2)`
* [!distinct](#distinct-list) `(list)`
* [!contains?](#contains-list-element) `(list element)`
+* [!some](#some-fn-list) `(fn list)`
+* [!every?](#every-fn-list) `(fn list)`
There are also anaphoric versions of these functions where that makes sense,
prefixed with two bangs instead of one.
@@ -205,6 +207,26 @@ or with `!compare-fn` if that's non-nil.
(!contains? '(1 2 3) 4) ;; => nil
```
+### !some `(fn list)`
+
+Returns the first non-nil value of (`fn` x) for any x in `list`, else nil.
+
+```cl
+(!some 'even? '(1 2 3)) ;; => t
+(!some 'even? '(1 3 5)) ;; => nil
+(!!some (= 0 (% it 2)) '(1 2 3)) ;; => t
+```
+
+### !every? `(fn list)`
+
+Returns t if (`fn` x) is non-nil for every x in `list`, else nil.
+
+```cl
+(!every? 'even? '(1 2 3)) ;; => nil
+(!every? 'even? '(2 4 6)) ;; => t
+(!!every? (= 0 (% it 2)) '(2 4 6)) ;; => t
+```
+
## Development
diff --git a/bang.el b/bang.el
index eb23382..8e01b94 100644
--- a/bang.el
+++ b/bang.el
@@ -168,6 +168,34 @@ or with `!compare-fn' if that's non-nil."
(setq lst (cdr lst)))
lst))))))
+(defmacro !!some (form list)
+ "Anaphoric form of `!some'."
+ `(let ((!--list ,list)
+ (!--any nil))
+ (while (and !--list (not !--any))
+ (let ((it (car !--list)))
+ (setq !--any ,form))
+ (setq !--list (cdr !--list)))
+ !--any))
+
+(defun !some (fn list)
+ "Returns the first non-nil value of (FN x) for any x in LIST, else nil."
+ (!!some (funcall fn it) list))
+
+(defmacro !!every? (form list)
+ "Anaphoric form of `!every?'."
+ `(let ((!--list ,list)
+ (!--all t))
+ (while (and !--all !--list)
+ (let ((it (car !--list)))
+ (setq !--all ,form))
+ (setq !--list (cdr !--list)))
+ (not (null !--all))))
+
+(defun !every? (fn list)
+ "Returns t if (FN x) is non-nil for every x in LIST, else nil."
+ (!!every? (funcall fn it) list))
+
(defvar !compare-fn nil
"Tests for equality use this function or `equal' if this is nil.
It should only be set using dynamic scope with a let, like:
diff --git a/examples.el b/examples.el
index d25afa5..6593cc6 100644
--- a/examples.el
+++ b/examples.el
@@ -80,3 +80,13 @@
(!contains? '(1 2 3) 4) => nil
(!contains? '() 1) => nil
(!contains? '() '()) => nil)
+
+(defexamples !some
+ (!some 'even? '(1 2 3)) => t
+ (!some 'even? '(1 3 5)) => nil
+ (!!some (= 0 (% it 2)) '(1 2 3)) => t)
+
+(defexamples !every?
+ (!every? 'even? '(1 2 3)) => nil
+ (!every? 'even? '(2 4 6)) => t
+ (!!every? (= 0 (% it 2)) '(2 4 6)) => t)
- [elpa] externals/dash 1b0d54f 044/426: Simplify readme generation., (continued)
- [elpa] externals/dash 1b0d54f 044/426: Simplify readme generation., Phillip Lord, 2015/08/04
- [elpa] externals/dash 26113a1 041/426: Add pre-commit.sh that runs tests and updates readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 78fc2a3 038/426: Quote and downcase params with digits in them too., Phillip Lord, 2015/08/04
- [elpa] externals/dash 32053cd 042/426: Update readme with development notes., Phillip Lord, 2015/08/04
- [elpa] externals/dash 0912aa2 048/426: Rename !uniq to !distinct, Phillip Lord, 2015/08/04
- [elpa] externals/dash 24262a1 054/426: Add file-local font-lock for defexamples and =>, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9312469 053/426: !keep, Phillip Lord, 2015/08/04
- [elpa] externals/dash 657ee8b 040/426: Slight improvements to docs., Phillip Lord, 2015/08/04
- [elpa] externals/dash 286d1d8 050/426: Show quotes around strings in examples., Phillip Lord, 2015/08/04
- [elpa] externals/dash db8a745 056/426: Add !each, Phillip Lord, 2015/08/04
- [elpa] externals/dash f59b480 055/426: Add !some and !every?,
Phillip Lord <=
- [elpa] externals/dash b337ef9 052/426: Sync examples-to-docs.el with the one in s.el, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2b89641 049/426: Remove needless duplication., Phillip Lord, 2015/08/04
- [elpa] externals/dash d83ea8a 061/426: Add installation instructions., Phillip Lord, 2015/08/04
- [elpa] externals/dash e1a362c 047/426: Update README to reflect changes in readme generation., Phillip Lord, 2015/08/04
- [elpa] externals/dash 5bd4593 067/426: Add note about !rpartial only working on Emacs 24+, Phillip Lord, 2015/08/04
- [elpa] externals/dash 05dec7a 062/426: Fix example., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6f17346 068/426: Don't run !rpartial tests on Emacsen <24, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4205e58 064/426: Add clojure threading macros, !-> and !->>, Phillip Lord, 2015/08/04
- [elpa] externals/dash cd2a793 069/426: Run travis-ci on both E23 and E24, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1a7ad85 065/426: Mention Melpa in README, Phillip Lord, 2015/08/04