emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 962e5b8 187/439: Merge pull request #33 from Fuco1


From: Phillip Lord
Subject: [elpa] externals/dash 962e5b8 187/439: Merge pull request #33 from Fuco1/reduce-r
Date: Tue, 04 Aug 2015 20:27:47 +0000

branch: externals/dash
commit 962e5b82f9f35c5e85eefa907ae5ff36c745852d
Merge: 1fc6679 d556884
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>

    Merge pull request #33 from Fuco1/reduce-r
    
    Add reduce-r, the right associative variant of reduce
---
 README.md       |   45 +++++++++++++++++++++++++++++++++++++++++----
 dash.el         |   39 +++++++++++++++++++++++++++++++++++++++
 dev/examples.el |   25 +++++++++++++++++++++----
 3 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index 0d11b7f..98954d7 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,9 @@ Or you can just dump `dash.el` in your load path somewhere.
 
 * [-map](#-map-fn-list) `(fn list)`
 * [-reduce-from](#-reduce-from-fn-initial-value-list) `(fn initial-value list)`
+* [-reduce-r-from](#-reduce-r-from-fn-initial-value-list) `(fn initial-value 
list)`
 * [-reduce](#-reduce-fn-list) `(fn list)`
+* [-reduce-r](#-reduce-r-fn-list) `(fn list)`
 * [-filter](#-filter-pred-list) `(pred list)`
 * [-remove](#-remove-pred-list) `(pred list)`
 * [-keep](#-keep-fn-list) `(fn list)`
@@ -121,9 +123,24 @@ In the anaphoric form `--reduce-from`, the accumulated 
value is
 exposed as `acc`.
 
 ```cl
-(-reduce-from '+ 7 '(1 2)) ;; => 10
-(-reduce-from (lambda (memo item) (+ memo item)) 7 '(1 2)) ;; => 10
-(--reduce-from (+ acc it) 7 '(1 2 3)) ;; => 13
+(-reduce-from '- 10 '(1 2 3)) ;; => 4
+(-reduce-from (lambda (memo item) (concat "(" memo " - " (int-to-string item) 
")")) "10" '(1 2 3)) ;; => "(((10 - 1) - 2) - 3)"
+(--reduce-from (concat acc " " it) "START" '("a" "b" "c")) ;; => "START a b c"
+```
+
+### -reduce-r-from `(fn initial-value list)`
+
+Replace conses with `fn`, nil with `initial-value` and evaluate
+the resulting expression. If `list` is empty, `initial-value` is
+returned and `fn` is not called.
+
+Note: this function works the same as `-reduce-from` but the
+operation associates from right instead of from left.
+
+```cl
+(-reduce-r-from '- 10 '(1 2 3)) ;; => -8
+(-reduce-r-from (lambda (item memo) (concat "(" (int-to-string item) " - " 
memo ")")) "10" '(1 2 3)) ;; => "(1 - (2 - (3 - 10)))"
+(--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) ;; => "a b c END"
 ```
 
 ### -reduce `(fn list)`
@@ -138,11 +155,31 @@ In the anaphoric form `--reduce`, the accumulated value is
 exposed as `acc`.
 
 ```cl
-(-reduce '+ '(1 2)) ;; => 3
+(-reduce '- '(1 2 3 4)) ;; => -8
 (-reduce (lambda (memo item) (format "%s-%s" memo item)) '(1 2 3)) ;; => 
"1-2-3"
 (--reduce (format "%s-%s" acc it) '(1 2 3)) ;; => "1-2-3"
 ```
 
+### -reduce-r `(fn list)`
+
+Replace conses with `fn` and evaluate the resulting expression.
+The final nil is ignored. If `list` contains no items, `fn` must
+accept no arguments as well, and reduce returns the result of
+calling `fn` with no arguments. If `list` has only 1 item, it is
+returned and `fn` is not called.
+
+The first argument of `fn` is the new item, the second is the
+accumulated value.
+
+Note: this function works the same as `-reduce` but the operation
+associates from right instead of from left.
+
+```cl
+(-reduce-r '- '(1 2 3 4)) ;; => -2
+(-reduce-r (lambda (item memo) (format "%s-%s" memo item)) '(1 2 3)) ;; => 
"3-2-1"
+(--reduce-r (format "%s-%s" acc it) '(1 2 3)) ;; => "3-2-1"
+```
+
 ### -filter `(pred list)`
 
 Returns a new list of the items in `list` for which `pred` returns a non-nil 
value.
diff --git a/dash.el b/dash.el
index b3c31b6..fc7dded 100644
--- a/dash.el
+++ b/dash.el
@@ -130,6 +130,41 @@ exposed as `acc`."
       (-reduce-from fn (car list) (cdr list))
     (funcall fn)))
 
+(defun -reduce-r-from (fn initial-value list)
+  "Replace conses with FN, nil with INITIAL-VALUE and evaluate
+the resulting expression. If LIST is empty, INITIAL-VALUE is
+returned and FN is not called.
+
+Note: this function works the same as `-reduce-from' but the
+operation associates from right instead of from left."
+  (if (not list) initial-value
+    (funcall fn (car list) (-reduce-r-from fn initial-value (cdr list)))))
+
+(defmacro --reduce-r-from (form initial-value list)
+  "Anaphoric version of `-reduce-r-from'."
+  `(-reduce-r-from (lambda (&optional it acc) ,form) ,initial-value ,list))
+
+(defun -reduce-r (fn list)
+  "Replace conses with FN and evaluate the resulting expression.
+The final nil is ignored. If LIST contains no items, FN must
+accept no arguments as well, and reduce returns the result of
+calling FN with no arguments. If LIST has only 1 item, it is
+returned and FN is not called.
+
+The first argument of FN is the new item, the second is the
+accumulated value.
+
+Note: this function works the same as `-reduce' but the operation
+associates from right instead of from left."
+  (cond
+   ((not list) (funcall fn))
+   ((not (cdr list)) (car list))
+   (t (funcall fn (car list) (-reduce-r fn (cdr list))))))
+
+(defmacro --reduce-r (form list)
+  "Anaphoric version of `-reduce-r'."
+  `(-reduce-r (lambda (&optional it acc) ,form) ,list))
+
 (defmacro --filter (form list)
   "Anaphoric form of `-filter'."
   (let ((r (make-symbol "result")))
@@ -840,6 +875,10 @@ Returns nil if N is less than 1."
                            "-reduce-from"
                            "--reduce"
                            "-reduce"
+                           "--reduce-r-from"
+                           "-reduce-r-from"
+                           "--reduce-r"
+                           "-reduce-r"
                            "--filter"
                            "-filter"
                            "-select"
diff --git a/dev/examples.el b/dev/examples.el
index 1f005b6..0bb9710 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -16,20 +16,37 @@
   (--map (concat it it) (three-letters)) => '("AA" "BB" "CC"))
 
 (defexamples -reduce-from
-  (-reduce-from '+ 7 '(1 2)) => 10
-  (-reduce-from (lambda (memo item) (+ memo item)) 7 '(1 2)) => 10
-  (--reduce-from (+ acc it) 7 '(1 2 3)) => 13
+  (-reduce-from '- 10 '(1 2 3)) => 4
+  (-reduce-from (lambda (memo item) 
+                   (concat "(" memo " - " (int-to-string item) ")")) "10" '(1 
2 3)) => "(((10 - 1) - 2) - 3)"
+  (--reduce-from (concat acc " " it) "START" '("a" "b" "c")) => "START a b c"
   (-reduce-from '+ 7 '()) => 7
   (-reduce-from '+ 7 '(1)) => 8)
 
+(defexamples -reduce-r-from
+  (-reduce-r-from '- 10 '(1 2 3)) => -8
+  (-reduce-r-from (lambda (item memo) 
+                   (concat "(" (int-to-string item) " - " memo ")")) "10" '(1 
2 3)) => "(1 - (2 - (3 - 10)))"
+  (--reduce-r-from (concat it " " acc) "END" '("a" "b" "c")) => "a b c END"
+  (-reduce-r-from '+ 7 '()) => 7
+  (-reduce-r-from '+ 7 '(1)) => 8)
+
 (defexamples -reduce
-  (-reduce '+ '(1 2)) => 3
+  (-reduce '- '(1 2 3 4)) => -8
   (-reduce (lambda (memo item) (format "%s-%s" memo item)) '(1 2 3)) => "1-2-3"
   (--reduce (format "%s-%s" acc it) '(1 2 3)) => "1-2-3"
   (-reduce '+ '()) => 0
   (-reduce '+ '(1)) => 1
   (--reduce (format "%s-%s" acc it) '()) => "nil-nil")
 
+(defexamples -reduce-r
+  (-reduce-r '- '(1 2 3 4)) => -2
+  (-reduce-r (lambda (item memo) (format "%s-%s" memo item)) '(1 2 3)) => 
"3-2-1"
+  (--reduce-r (format "%s-%s" acc it) '(1 2 3)) => "3-2-1"
+  (-reduce-r '+ '()) => 0
+  (-reduce-r '+ '(1)) => 1
+  (--reduce-r (format "%s-%s" it acc) '()) => "nil-nil")
+
 (defexamples -filter
   (-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4)) => '(2 4)
   (-filter 'even? '(1 2 3 4)) => '(2 4)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]