[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)
- [elpa] externals/dash 12291f3 191/426: Add -product function., (continued)
- [elpa] externals/dash 12291f3 191/426: Add -product function., Phillip Lord, 2015/08/04
- [elpa] externals/dash ff3d7bd 190/426: Add -sum function., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f9fca3 177/426: Merge pull request #30 from shosti/insert-at-opt, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e41bed 192/426: Merge pull request #35 from rejeep/sum-and-product, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3226100 194/426: Indent according to emacs lisp standard, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9f156a6 193/426: Release 1.5.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash a3faf7c 183/426: Release 1.3.2, Phillip Lord, 2015/08/04
- [elpa] externals/dash e07cef6 196/426: Adding -first-item and -last-item. [magnars/dash.el#17], Phillip Lord, 2015/08/04
- [elpa] externals/dash 50659cc 189/426: Release 1.4.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4164908 197/426: Add -min, -max, -min-by and -max-by., Phillip Lord, 2015/08/04
- [elpa] externals/dash 2b20088 195/426: Add -juxt,
Phillip Lord <=
- [elpa] externals/dash a3b2fdb 198/426: Merge pull request #38 from rejeep/min-and-max, Phillip Lord, 2015/08/04
- [elpa] externals/dash 22d2c2d 199/426: Release 1.6.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash b0ff280 205/426: Updating docs--signature to handle aliases and subrs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 506401e 203/426: Merge pull request #39 from Fuco1/rotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5b5dab5 200/426: Fix typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9bd656e 202/426: Add -rotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash 970728e 201/426: Actually update README :P, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8b17154 204/426: Release 1.7.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0db2f5a 208/426: Fix second example for -last/first-item, Phillip Lord, 2015/08/04
- [elpa] externals/dash 788573e 206/426: Merge pull request #36 from Wilfred/master, Phillip Lord, 2015/08/04