[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash ed2e447 015/439: !reduce macro
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash ed2e447 015/439: !reduce macro |
Date: |
Tue, 04 Aug 2015 20:25:53 +0000 |
branch: externals/dash
commit ed2e44722b41b635699dd4745c4de37042e55d29
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
!reduce macro
---
bang.el | 28 ++++++++++++++++++++++------
tests.el | 14 ++++++++++++++
2 files changed, 36 insertions(+), 6 deletions(-)
diff --git a/bang.el b/bang.el
index 46689e4..a410bb6 100644
--- a/bang.el
+++ b/bang.el
@@ -27,11 +27,6 @@
(eval-when-compile (require 'cl))
-(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:
-(let ((!compare-fn =)) (!union numbers1 numbers2 numbers3)")
-
(defmacro !filter (form-or-fn list)
`(let ((!--list ,list)
(!--result '()))
@@ -47,11 +42,27 @@ It should only be set using dynamic scope with a let, like:
`(mapcar #',form-or-fn ,list)
`(mapcar #'(lambda (it) ,form-or-fn) ,list)))
+(defmacro !reduce-from (form-or-fn initial-value list)
+ `(let ((!--list ,list)
+ (!--acc ,initial-value))
+ (while !--list
+ (let ((it (car !--list))
+ (acc !--acc))
+ (setq !--acc ,(if (functionp form-or-fn) (list form-or-fn 'acc 'it)
(list 'progn form-or-fn)))
+ (setq !--list (cdr !--list))))
+ !--acc))
+
+(defmacro !reduce (form-or-fn list)
+ (if (eval list)
+ `(!reduce-from ,form-or-fn ,(car (eval list)) ',(cdr (eval list)))
+ (if (functionp form-or-fn)
+ (list form-or-fn)
+ `(let (acc it) ,form-or-fn))))
(defun !concat (list)
(apply 'concatenate 'list list))
-(defalias '!select 'remove-if-not)
+(defalias '!select '!filter)
(defalias '!reject 'remove-if)
(defalias '!partial 'apply-partially)
@@ -92,5 +103,10 @@ or with `!compare-fn' if that's non-nil."
(setq lst (cdr lst)))
lst))))
+(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:
+(let ((!compare-fn =)) (!union numbers1 numbers2 numbers3)")
+
(provide 'bang)
;;; bang.el ends here
diff --git a/tests.el b/tests.el
index 9600cb6..367ebeb 100644
--- a/tests.el
+++ b/tests.el
@@ -16,6 +16,20 @@
(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 reduce ()
+ "`!reduce' takes a list and applies the function over them to create one
result"
+ (should (equal (!reduce + '()) 0))
+ (should (equal (!reduce + '(1)) 1))
+ (should (equal (!reduce + '(1 2)) 3))
+ (should (equal (!reduce-from + 7 '()) 7))
+ (should (equal (!reduce-from + 7 '(1)) 8))
+ (should (equal (!reduce-from + 7 '(1 2)) 10))
+
+ (should (equal (!reduce (lambda (memo item) (format "%s-%s" memo item)) '(1
2 3)) "1-2-3"))
+ (should (equal (!reduce (format "%s-%s" acc it) '(1 2 3)) "1-2-3"))
+ (should (equal (!reduce (format "%s-%s" acc it) '()) "nil-nil"))
+)
+
(ert-deftest difference ()
"`!difference' returns a new list of only elements in list1 that are not in
list2."
(should (equal (!difference '() '()) '()))
- [elpa] externals/dash 14673a6 007/439: Use nreverse for performance reasons., (continued)
- [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, 2015/08/04
- [elpa] externals/dash ed2e447 015/439: !reduce macro,
Phillip Lord <=
- [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
- [elpa] externals/dash 754dd0d 024/439: Create stand-alone anaphoric macros, Phillip Lord, 2015/08/04