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

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

[elpa] externals/dash 2b20088 195/426: Add -juxt


From: Phillip Lord
Subject: [elpa] externals/dash 2b20088 195/426: Add -juxt
Date: Tue, 04 Aug 2015 19:37:40 +0000

branch: externals/dash
commit 2b200884cacd6856f52ccbea8fd3aee41856e8cd
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>

    Add -juxt
---
 README.md       |   59 ++++++++++++++++++++++++++++++++++--------------------
 dash.el         |   14 +++++++++++++
 dev/examples.el |   34 +++++++++++++++++--------------
 3 files changed, 70 insertions(+), 37 deletions(-)

diff --git a/README.md b/README.md
index 04996e1..51209ae 100644
--- a/README.md
+++ b/README.md
@@ -27,6 +27,8 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-mapcat](#-mapcat-fn-list) `(fn list)`
 * [-cons*](#-cons-rest-args) `(&rest args)`
 * [-count](#-count-pred-list) `(pred list)`
+* [-sum](#-sum-list) `(list)`
+* [-product](#-product-list) `(list)`
 * [-any?](#-any-pred-list) `(pred list)`
 * [-all?](#-all-pred-list) `(pred list)`
 * [-none?](#-none-pred-list) `(pred list)`
@@ -65,6 +67,7 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-sort](#-sort-predicate-list) `(predicate list)`
 * [-partial](#-partial-fn-rest-args) `(fn &rest args)`
 * [-rpartial](#-rpartial-fn-rest-args) `(fn &rest args)`
+* [-juxt](#-juxt-rest-fns) `(&rest fns)`
 * [-applify](#-applify-fn) `(fn)`
 * [->](#--x-optional-form-rest-more) `(x &optional form &rest more)`
 * [->>](#--x-form-rest-more) `(x form &rest more)`
@@ -75,8 +78,6 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [-if-let*](#-if-let-vars-vals-then-optional-else) `(vars-vals then &optional 
else)`
 * [!cons](#-cons-car-cdr) `(car cdr)`
 * [!cdr](#-cdr-list) `(list)`
-* [-sum](#-sum-list) `(list)`
-* [-product](#-product-list) `(list)`
 
 There are also anaphoric versions of these functions where that makes sense,
 prefixed with two dashes instead of one.
@@ -294,6 +295,26 @@ Counts the number of items in `list` where (`pred` item) 
is non-nil.
 (--count (< it 4) '(1 2 3 4)) ;; => 3
 ```
 
+### -sum `(list)`
+
+Return the sum of `list`.
+
+```cl
+(-sum '()) ;; => 0
+(-sum '(1)) ;; => 1
+(-sum '(1 2 3)) ;; => 6
+```
+
+### -product `(list)`
+
+Return the product of `list`.
+
+```cl
+(-product '()) ;; => 1
+(-product '(1)) ;; => 1
+(-product '(1 2 3)) ;; => 6
+```
+
 ### -any? `(pred list)`
 
 Returns t if (`pred` x) is non-nil for any x in `list`, else nil.
@@ -717,6 +738,20 @@ Requires Emacs 24 or higher.
 (funcall (-rpartial '- 5 2) 10) ;; => 3
 ```
 
+### -juxt `(&rest fns)`
+
+Takes a list of functions and returns a fn that is the
+juxtaposition of those fns. The returned fn takes a variable
+number of args, and returns a list containing the result of
+applying each fn to the args (left-to-right).
+
+Requires Emacs 24 or higher.
+
+```cl
+(funcall (-juxt '+ '-) 3 5) ;; => '(8 -2)
+(-map (-juxt 'identity 'square) '(1 2 3)) ;; => '((1 1) (2 4) (3 9))
+```
+
 ### -applify `(fn)`
 
 Changes an n-arity function `fn` to a 1-arity function that
@@ -827,26 +862,6 @@ Destructive: Sets `list` to the cdr of `list`.
 (let ((l '(3 5))) (!cdr l) l) ;; => '(5)
 ```
 
-### -sum `(list)`
-
-Return the sum of `list`.
-
-```cl
-(-sum '()) ;; => 0
-(-sum '(1)) ;; => 1
-(-sum '(1 2 3)) ;; => 6
-```
-
-### -product `(list)`
-
-Return the product of `list`.
-
-```cl
-(-product '()) ;; => 1
-(-product '(1)) ;; => 1
-(-product '(1 2 3)) ;; => 6
-```
-
 
 ## Contribute
 
diff --git a/dash.el b/dash.el
index da4b03d..31e9a83 100644
--- a/dash.el
+++ b/dash.el
@@ -693,6 +693,19 @@ Requires Emacs 24 or higher."
   `(closure (t) (&rest args)
             (apply ',fn (append args ',args))))
 
+(defun -juxt (&rest fns)
+  "Takes a list of functions and returns a fn that is the
+juxtaposition of those fns. The returned fn takes a variable
+number of args, and returns a list containing the result of
+applying each fn to the args (left-to-right).
+
+Requires Emacs 24 or higher."
+  (let ((r (make-symbol "result")))
+    `(closure (t) (&rest args)
+              (let (,r)
+                (--each ',fns (!cons (apply it args) ,r))
+                (nreverse ,r)))))
+
 (defun -applify (fn)
   "Changes an n-arity function FN to a 1-arity function that
 expects a list with n items as arguments"
@@ -966,6 +979,7 @@ Returns nil if N is less than 1."
                            "-replace-where"
                            "-partial"
                            "-rpartial"
+                           "-juxt"
                            "->"
                            "->>"
                            "-->"
diff --git a/dev/examples.el b/dev/examples.el
index 2730583..e035320 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -17,16 +17,16 @@
 
 (defexamples -reduce-from
   (-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 (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 (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)
@@ -99,6 +99,16 @@
   (-count 'even? '(1 2 3 4 5)) => 2
   (--count (< it 4) '(1 2 3 4)) => 3)
 
+(defexamples -sum
+  (-sum '()) => 0
+  (-sum '(1)) => 1
+  (-sum '(1 2 3)) => 6)
+
+(defexamples -product
+  (-product '()) => 1
+  (-product '(1)) => 1
+  (-product '(1 2 3)) => 6)
+
 (defexamples -any?
   (-any? 'even? '(1 2 3)) => t
   (-any? 'even? '(1 3 5)) => nil
@@ -288,7 +298,11 @@
 (unless (version< emacs-version "24")
   (defexamples -rpartial
     (funcall (-rpartial '- 5) 8) => 3
-    (funcall (-rpartial '- 5 2) 10) => 3))
+    (funcall (-rpartial '- 5 2) 10) => 3)
+
+  (defexamples -juxt
+    (funcall (-juxt '+ '-) 3 5) => '(8 -2)
+    (-map (-juxt 'identity 'square) '(1 2 3)) => '((1 1) (2 4) (3 9))))
 
 (defexamples -applify
   (-map (-applify '+) '((1 1 1) (1 2 3) (5 5 5))) => '(3 6 15)
@@ -336,13 +350,3 @@
 (defexamples !cdr
   (let ((l '(3))) (!cdr l) l) => '()
   (let ((l '(3 5))) (!cdr l) l) => '(5))
-
-(defexamples -sum
-  (-sum '()) => 0
-  (-sum '(1)) => 1
-  (-sum '(1 2 3)) => 6)
-
-(defexamples -product
-  (-product '()) => 1
-  (-product '(1)) => 1
-  (-product '(1 2 3)) => 6)



reply via email to

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