[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 3e0ef58 012/439: !filter can now take either a fun
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 3e0ef58 012/439: !filter can now take either a function or form (anaphoric) |
Date: |
Tue, 04 Aug 2015 20:25:51 +0000 |
branch: externals/dash
commit 3e0ef58cd4ce5ec721330ac498091e71337b8f47
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
!filter can now take either a function or form (anaphoric)
---
README.md | 16 ++++++++++++++++
bang.el | 11 +++++------
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index eef2dba..b6c806d 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,22 @@ Right now it relies on `cl` which should be one of the very
first things we fix.
This is so much a work in progress that you should definitely not be using it
yet.
+## Anaphoric functions
+
+While `!filter` takes a function to filter the list by, you can also pass
+it a forms - which will then be executed with `it` exposed as the list item.
+Here's an example:
+
+ (!filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) ;; normal version
+
+ (!filter (= 0 (% it 2)) '(1 2 3 4)) ;; anaphoric version
+
+of course the original can also be written like
+
+ (!filter 'even? '(1 2 3 4))
+
+which demonstrates the usefulness of both versions.
+
## License
Copyright (C) 2012 Magnar Sveen, Joel McCracken
diff --git a/bang.el b/bang.el
index e552988..dc330f9 100644
--- a/bang.el
+++ b/bang.el
@@ -37,7 +37,6 @@ It should only be set using dynamic scope with a let, like:
(defalias '!map 'mapcar)
-(defalias '!filter 'remove-if-not)
(defalias '!select 'remove-if-not)
(defalias '!reject 'remove-if)
@@ -46,11 +45,11 @@ It should only be set using dynamic scope with a let, like:
(defun !mapcat (fn list)
(!concat (!map fn list)))
-(defmacro !-filter (list &rest forms)
+(defmacro !filter (form-or-fn list)
`(let (!--result)
(while list
(let ((it (car list)))
- (when (progn ,@forms)
+ (when ,(if (functionp form-or-fn) (list form-or-fn 'it) (list 'progn
form-or-fn))
(setq !--result (cons it !--result))))
(setq list (cdr list)))
(nreverse !--result)))
@@ -59,19 +58,19 @@ It should only be set using dynamic scope with a let, like:
"Return a new list with all duplicates removed.
The test for equality is done with `equal',
or with `!compare-fn' if that's non-nil."
- (!-filter list (not (!contains-p !--result it))))
+ (!filter (not (!contains-p !--result it)) list))
(defun !intersection (list list2)
"Return a new list containing only the elements that are members of both
LIST and LIST2.
The test for equality is done with `equal',
or with `!compare-fn' if that's non-nil."
- (!-filter list (!contains-p list2 it)))
+ (!filter (!contains-p list2 it) list))
(defun !difference (list list2)
"Return a new list with only the members of LIST that are not in LIST2.
The test for equality is done with `equal',
or with `!compare-fn' if that's non-nil."
- (!-filter list (not (!contains-p list2 it))))
+ (!filter (not (!contains-p list2 it)) list))
(defun !contains-p (list element)
"Return whether LIST contains ELEMENT.
- [elpa] externals/dash 9a34da8 004/439: Add config for travis-ci, (continued)
- [elpa] externals/dash 9a34da8 004/439: Add config for travis-ci, Phillip Lord, 2015/08/04
- [elpa] externals/dash 302c8ea 001/439: Initial commit., Phillip Lord, 2015/08/04
- [elpa] externals/dash 14673a6 007/439: Use nreverse for performance reasons., Phillip Lord, 2015/08/04
- [elpa] externals/dash 7b52951 008/439: Add watchr script to get autotest, Phillip Lord, 2015/08/04
- [elpa] externals/dash 93957f0 006/439: Add travis status icon to README, Phillip Lord, 2015/08/04
- [elpa] externals/dash ad44aee 009/439: Add more tests., Phillip Lord, 2015/08/04
- [elpa] externals/dash 065f76b 005/439: Use ert.el to run on Emacs 23 as well., Phillip Lord, 2015/08/04
- [elpa] externals/dash e720428 010/439: Extract basic iteration form into macro., Phillip Lord, 2015/08/04
- [elpa] externals/dash c229ba8 011/439: Rewrite to anaphoric filter function., Phillip Lord, 2015/08/04
- [elpa] externals/dash d870282 013/439: Add some tests, and fix the bugs they uncovered., Phillip Lord, 2015/08/04
- [elpa] externals/dash 3e0ef58 012/439: !filter can now take either a function or form (anaphoric),
Phillip Lord <=
- [elpa] externals/dash b24d056 014/439: !map macro, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed2e447 015/439: !reduce macro, Phillip Lord, 2015/08/04
- [elpa] externals/dash f9471e4 016/439: !concat done - one less 'cl dependency, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4201ecd 017/439: Out with remove-if, in with !reject, Phillip Lord, 2015/08/04
- [elpa] externals/dash f5b16e3 018/439: Make !partial have the same quote-less API as the rest of bang., Phillip Lord, 2015/08/04
- [elpa] externals/dash a02b6c1 021/439: Add note about no 'cl required., Phillip Lord, 2015/08/04
- [elpa] externals/dash 07de30e 019/439: !mapcat macro, and with that: goodbye 'cl!, Phillip Lord, 2015/08/04
- [elpa] externals/dash 52815c5 020/439: Add documentation, Phillip Lord, 2015/08/04
- [elpa] externals/dash 73204ca 022/439: Change !contains-p to !contains? to match clojure api., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6c39876 003/439: Set up test framework with one basic test, Phillip Lord, 2015/08/04