[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash b24d056 014/439: !map macro
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash b24d056 014/439: !map macro |
Date: |
Tue, 04 Aug 2015 20:25:52 +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] externals/dash 302c8ea 001/439: Initial commit., (continued)
- [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, 2015/08/04
- [elpa] externals/dash b24d056 014/439: !map macro,
Phillip Lord <=
- [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
- [elpa] externals/dash c0f2c8f 026/439: Test that it works with lexical binding., Phillip Lord, 2015/08/04