[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash b24d056 014/426: !map macro
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash b24d056 014/426: !map macro |
Date: |
Tue, 04 Aug 2015 19:36:23 +0000 |
branch: externals/dash
commit b24d05608143bda6542d82eef6cb70253270d280
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
!map macro
---
bang.el | 28 ++++++++++++++++------------
tests.el | 7 +++++++
2 files changed, 23 insertions(+), 12 deletions(-)
diff --git a/bang.el b/bang.el
index 9f67ed0..46689e4 100644
--- a/bang.el
+++ b/bang.el
@@ -32,11 +32,25 @@
It should only be set using dynamic scope with a let, like:
(let ((!compare-fn =)) (!union numbers1 numbers2 numbers3)")
+(defmacro !filter (form-or-fn list)
+ `(let ((!--list ,list)
+ (!--result '()))
+ (while !--list
+ (let ((it (car !--list)))
+ (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)))
+
+(defmacro !map (form-or-fn list)
+ (if (functionp form-or-fn)
+ `(mapcar #',form-or-fn ,list)
+ `(mapcar #'(lambda (it) ,form-or-fn) ,list)))
+
+
(defun !concat (list)
(apply 'concatenate 'list list))
-(defalias '!map 'mapcar)
-
(defalias '!select 'remove-if-not)
(defalias '!reject 'remove-if)
@@ -45,16 +59,6 @@ It should only be set using dynamic scope with a let, like:
(defun !mapcat (fn list)
(!concat (!map fn list)))
-(defmacro !filter (form-or-fn list)
- `(let ((!--list ,list)
- (!--result '()))
- (while !--list
- (let ((it (car !--list)))
- (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)))
-
(defun !uniq (list)
"Return a new list with all duplicates removed.
The test for equality is done with `equal',
diff --git a/tests.el b/tests.el
index 5f1a9dc..9600cb6 100644
--- a/tests.el
+++ b/tests.el
@@ -2,6 +2,7 @@
(require 'bang)
(defun even? (num) (= 0 (% num 2)))
+(defun square (num) (* num num))
(ert-deftest filter ()
"`!filter' returns a new list of only those elements where the predicate was
non-nil."
@@ -9,6 +10,12 @@
(should (equal (!filter (= 0 (% it 2)) '(1 2 3 4)) '(2 4)))
(should (equal (!filter even? '(1 2 3 4)) '(2 4))))
+(ert-deftest map ()
+ "`!map' returns a new list with the results of calling the function on each
element."
+ (should (equal (!map (lambda (num) (* num num)) '(1 2 3 4)) '(1 4 9 16)))
+ (should (equal (!map (* it it) '(1 2 3 4)) '(1 4 9 16)))
+ (should (equal (!map square '(1 2 3 4)) '(1 4 9 16))))
+
(ert-deftest difference ()
"`!difference' returns a new list of only elements in list1 that are not in
list2."
(should (equal (!difference '() '()) '()))
- [elpa] branch externals/dash created (now 19fbc24), Phillip Lord, 2015/08/04
- [elpa] externals/dash 300fbc6 002/426: Remove lisp-comments from README, Phillip Lord, 2015/08/04
- [elpa] externals/dash 93957f0 006/426: Add travis status icon to README, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7b52951 008/426: Add watchr script to get autotest, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9a34da8 004/426: Add config for travis-ci, Phillip Lord, 2015/08/04
- [elpa] externals/dash e720428 010/426: Extract basic iteration form into macro., Phillip Lord, 2015/08/04
- [elpa] externals/dash 14673a6 007/426: Use nreverse for performance reasons., Phillip Lord, 2015/08/04
- [elpa] externals/dash 065f76b 005/426: Use ert.el to run on Emacs 23 as well., Phillip Lord, 2015/08/04
- [elpa] externals/dash 3e0ef58 012/426: !filter can now take either a function or form (anaphoric), Phillip Lord, 2015/08/04
- [elpa] externals/dash d870282 013/426: Add some tests, and fix the bugs they uncovered., Phillip Lord, 2015/08/04
- [elpa] externals/dash b24d056 014/426: !map macro,
Phillip Lord <=
- [elpa] externals/dash f9471e4 016/426: !concat done - one less 'cl dependency, Phillip Lord, 2015/08/04
- [elpa] externals/dash 302c8ea 001/426: Initial commit., Phillip Lord, 2015/08/04
- [elpa] externals/dash 07de30e 019/426: !mapcat macro, and with that: goodbye 'cl!, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4201ecd 017/426: Out with remove-if, in with !reject, Phillip Lord, 2015/08/04
- [elpa] externals/dash c229ba8 011/426: Rewrite to anaphoric filter function., Phillip Lord, 2015/08/04
- [elpa] externals/dash 73204ca 022/426: Change !contains-p to !contains? to match clojure api., Phillip Lord, 2015/08/04
- [elpa] externals/dash ad44aee 009/426: Add more tests., Phillip Lord, 2015/08/04
- [elpa] externals/dash f5b16e3 018/426: Make !partial have the same quote-less API as the rest of bang., Phillip Lord, 2015/08/04
- [elpa] externals/dash a02b6c1 021/426: Add note about no 'cl required., Phillip Lord, 2015/08/04
- [elpa] externals/dash 754dd0d 024/426: Create stand-alone anaphoric macros, Phillip Lord, 2015/08/04