master
Magnar Sveen 14 years ago
parent f59b480a49
commit db8a7454dc
  1. 11
      README.md
  2. 12
      bang.el
  3. 5
      examples.el

@ -25,6 +25,7 @@ This is so much a work in progress that you should definitely not be using it ye
* [!contains?](#contains-list-element) `(list element)` * [!contains?](#contains-list-element) `(list element)`
* [!some](#some-fn-list) `(fn list)` * [!some](#some-fn-list) `(fn list)`
* [!every?](#every-fn-list) `(fn list)` * [!every?](#every-fn-list) `(fn list)`
* [!each](#each-list-fn) `(list fn)`
There are also anaphoric versions of these functions where that makes sense, There are also anaphoric versions of these functions where that makes sense,
prefixed with two bangs instead of one. prefixed with two bangs instead of one.
@ -227,6 +228,16 @@ Returns t if (`fn` x) is non-nil for every x in `list`, else nil.
(!!every? (= 0 (% it 2)) '(2 4 6)) ;; => t (!!every? (= 0 (% it 2)) '(2 4 6)) ;; => t
``` ```
### !each `(list fn)`
Calls `fn` with every item in `list`. Returns nil, used for side-effects only.
```cl
(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s))))) ;; => nil
(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s)))) s) ;; => '(3 2 1)
(let (s) (!!each '(1 2 3) (setq s (cons it s))) s) ;; => '(3 2 1)
```
## Development ## Development

@ -196,6 +196,18 @@ or with `!compare-fn' if that's non-nil."
"Returns t if (FN x) is non-nil for every x in LIST, else nil." "Returns t if (FN x) is non-nil for every x in LIST, else nil."
(!!every? (funcall fn it) list)) (!!every? (funcall fn it) list))
(defmacro !!each (list form)
"Anaphoric form of `!each'."
`(let ((!--list ,list))
(while !--list
(let ((it (car !--list)))
,form)
(setq !--list (cdr !--list)))))
(defun !each (list fn)
"Calls FN with every item in LIST. Returns nil, used for side-effects only."
(!!each list (funcall fn it)))
(defvar !compare-fn nil (defvar !compare-fn nil
"Tests for equality use this function or `equal' if this is 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: It should only be set using dynamic scope with a let, like:

@ -90,3 +90,8 @@
(!every? 'even? '(1 2 3)) => nil (!every? 'even? '(1 2 3)) => nil
(!every? 'even? '(2 4 6)) => t (!every? 'even? '(2 4 6)) => t
(!!every? (= 0 (% it 2)) '(2 4 6)) => t) (!!every? (= 0 (% it 2)) '(2 4 6)) => t)
(defexamples !each
(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s))))) => nil
(let (s) (!each '(1 2 3) (lambda (item) (setq s (cons item s)))) s) => '(3 2 1)
(let (s) (!!each '(1 2 3) (setq s (cons it s))) s) => '(3 2 1))

Loading…
Cancel
Save