[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash ed2e447 015/426: !reduce macro
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash ed2e447 015/426: !reduce macro |
Date: |
Tue, 04 Aug 2015 19:36:24 +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 73204ca 022/426: Change !contains-p to !contains? to match clojure api., (continued)
- [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
- [elpa] externals/dash 9e74cc0 027/426: Remove dead code., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8eab3d1 028/426: Slight improvements to map., Phillip Lord, 2015/08/04
- [elpa] externals/dash 1997e52 031/426: Treat dashes in docstring better., Phillip Lord, 2015/08/04
- [elpa] externals/dash 52815c5 020/426: Add documentation, Phillip Lord, 2015/08/04
- [elpa] externals/dash d2dfb11 023/426: Use double-bang for anaphoric functions, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed2e447 015/426: !reduce macro,
Phillip Lord <=
- [elpa] externals/dash 6f0636f 032/426: Show only three first examples per function., Phillip Lord, 2015/08/04
- [elpa] externals/dash c0f2c8f 026/426: Test that it works with lexical binding., Phillip Lord, 2015/08/04
- [elpa] externals/dash 41d77ec 034/426: Add function list to readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 06efd3c 039/426: Slight improvements to docs., Phillip Lord, 2015/08/04
- [elpa] externals/dash 85016d3 029/426: Fix quoted forms like `' in docs., Phillip Lord, 2015/08/04
- [elpa] externals/dash 214a7db 030/426: Quote and lowercase parameter names in docstring., Phillip Lord, 2015/08/04
- [elpa] externals/dash de5ff6a 037/426: Simplify quotes in readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 722fea6 033/426: Include docs in README, Phillip Lord, 2015/08/04
- [elpa] externals/dash ec27b47 025/426: Turn tests into examples that can both be tested and turned into docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4cc4147 035/426: Fix typo, Phillip Lord, 2015/08/04