[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 754dd0d 024/439: Create stand-alone anaphoric macr
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 754dd0d 024/439: Create stand-alone anaphoric macros |
Date: |
Tue, 04 Aug 2015 20:25:59 +0000 |
branch: externals/dash
commit 754dd0d1fbd2cdc07caa012ae5c4493beba65f02
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Create stand-alone anaphoric macros
- no need for lexical scope
- support Emacs 23
---
bang.el | 51 ++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 38 insertions(+), 13 deletions(-)
diff --git a/bang.el b/bang.el
index 5b1bc50..1094f4e 100644
--- a/bang.el
+++ b/bang.el
@@ -1,4 +1,4 @@
-;;; bang.el --- A modern list library for Emacs -*- lexical-binding: t -*-
+;;; bang.el --- A modern list library for Emacs
;; Copyright (C) 2012 Magnar Sveen, Joel McCracken
@@ -33,8 +33,8 @@
(defun !reduce-from (fn initial-value list)
"Returns the result of applying FN to INITIAL-VALUE and the
first item in LIST, then applying FN to that result and the 2nd
-item, etc. If INITIAL-VALUE contains no items, returns
-INITIAL-VALUE and FN is not called."
+item, etc. If LIST contains no items, returns INITIAL-VALUE and
+FN is not called."
(let ((acc initial-value))
(while list
(setq acc (funcall fn acc (car list)))
@@ -42,7 +42,18 @@ INITIAL-VALUE and FN is not called."
acc))
(defmacro !!reduce-from (form initial-value list)
- `(!reduce-from (lambda (acc it) ,form) ,initial-value ,list))
+ "Anaphoric form of `!reduce'. Returns the result of applying
+FORM to INITIAL-VALUE and the first item in LIST, then applying
+FORM to that result and the 2nd item, etc. If INITIAL-VALUE
+contains no items, returns INITIAL-VALUE and FORM is not called."
+ `(let ((!--list ,list)
+ (!--acc ,initial-value))
+ (while !--list
+ (let ((it (car !--list))
+ (acc !--acc))
+ (setq !--acc ,form))
+ (setq !--list (cdr !--list)))
+ !--acc))
(defun !reduce (fn list)
"Returns the result of applying FN to the first 2 items in LIST,
@@ -55,7 +66,15 @@ LIST has only 1 item, it is returned and FN is not called."
(funcall fn)))
(defmacro !!reduce (form list)
- `(!reduce (lambda (&optional acc it) ,form) ,list))
+ "Returns the result of applying FORM to the first 2 items in LIST,
+then applying FORM to that result and the 3rd item, etc. If
+LIST contains no items, FORM must accept no arguments as
+well, and reduce returns the result of calling FORM with no
+arguments. If LIST has only 1 item, it is returned and FORM
+is not called."
+ (if (eval list)
+ `(!!reduce-from ,form ,(car (eval list)) ',(cdr (eval list)))
+ `(let (acc it) ,form)))
(defun !filter (fn list)
"Returns a new list of the items in LIST for which FN returns a non-nil
value."
@@ -67,13 +86,22 @@ LIST has only 1 item, it is returned and FN is not called."
(nreverse result)))
(defmacro !!filter (form list)
- `(!filter (lambda (it) ,form) ,list))
+ "Returns a new list of the items in LIST for which FORM returns a non-nil
value."
+ `(let ((!--list ,list)
+ (!--result '()))
+ (while !--list
+ (let ((it (car !--list)))
+ (when ,form
+ (setq !--result (cons it !--result))))
+ (setq !--list (cdr !--list)))
+ (nreverse !--result)))
(defun !remove (fn list)
"Returns a new list of the items in LIST for which FN returns nil."
(!!filter (not (funcall fn it)) list))
(defmacro !!remove (form list)
+ "Returns a new list of the items in LIST for which FORM returns nil."
`(!!filter (not ,form) ,list))
(defun !concat (&rest lists)
@@ -87,7 +115,9 @@ Thus function FN should return a collection."
(apply '!concat (!map fn list)))
(defmacro !!mapcat (form list)
- `(!mapcat (lambda (it) ,form) ,list))
+ "Returns the result of applying concat to the result of applying map to FORM
and LIST.
+Thus function FORM should return a collection."
+ `(apply '!concat (!!map ,form ,list)))
(defalias '!partial 'apply-partially)
@@ -95,12 +125,7 @@ Thus function FN should return a collection."
"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."
- (let ((result '()))
- (while list
- (when (not (!contains? result (car list)))
- (setq result (cons (car list) result)))
- (setq list (cdr list)))
- (nreverse result)))
+ (!!filter (not (!contains? !--result it)) list))
(defun !intersection (list list2)
"Return a new list containing only the elements that are members of both
LIST and LIST2.
- [elpa] externals/dash ed2e447 015/439: !reduce macro, (continued)
- [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
- [elpa] externals/dash 754dd0d 024/439: Create stand-alone anaphoric macros,
Phillip Lord <=
- [elpa] externals/dash 9e74cc0 027/439: Remove dead code., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8eab3d1 028/439: Slight improvements to map., Phillip Lord, 2015/08/04
- [elpa] externals/dash d2dfb11 023/439: Use double-bang for anaphoric functions, Phillip Lord, 2015/08/04
- [elpa] externals/dash 85016d3 029/439: Fix quoted forms like `' in docs., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6f0636f 032/439: Show only three first examples per function., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4cc4147 035/439: Fix typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash 41d77ec 034/439: Add function list to readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 214a7db 030/439: Quote and lowercase parameter names in docstring., Phillip Lord, 2015/08/04
- [elpa] externals/dash ec27b47 025/439: Turn tests into examples that can both be tested and turned into docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1997e52 031/439: Treat dashes in docstring better., Phillip Lord, 2015/08/04