[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 2a7fbdc 218/439: Update -max-by & friends
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 2a7fbdc 218/439: Update -max-by & friends |
Date: |
Tue, 04 Aug 2015 20:28:11 +0000 |
branch: externals/dash
commit 2a7fbdc507e620d8de82d497d7ba0180b1a494c5
Author: Fuco1 <address@hidden>
Commit: Magnar Sveen <address@hidden>
Update -max-by & friends
---
README.md | 56 +++++++++++++++++++++++++-------------------
dash.el | 70 +++++++++++++++++++++++++-----------------------------
dev/examples.el | 28 +++++++++++-----------
3 files changed, 78 insertions(+), 76 deletions(-)
diff --git a/README.md b/README.md
index 6261ac6..a0cab04 100644
--- a/README.md
+++ b/README.md
@@ -29,10 +29,10 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-count](#-count-pred-list) `(pred list)`
* [-sum](#-sum-list) `(list)`
* [-product](#-product-list) `(list)`
-* [-min](#-min-x-rest-xs) `(x &rest xs)`
-* [-min-by](#-min-by-pred-list) `(pred list)`
-* [-max](#-max-x-rest-xs) `(x &rest xs)`
-* [-max-by](#-max-by-pred-list) `(pred list)`
+* [-min](#-min-list) `(list)`
+* [-min-by](#-min-by-comparator-list) `(comparator list)`
+* [-max](#-max-list) `(list)`
+* [-max-by](#-max-by-comparator-list) `(comparator list)`
* [-any?](#-any-pred-list) `(pred list)`
* [-all?](#-all-pred-list) `(pred list)`
* [-none?](#-none-pred-list) `(pred list)`
@@ -329,44 +329,52 @@ Return the product of `list`.
(-product '(1 2 3)) ;; => 6
```
-### -min `(x &rest xs)`
+### -min `(list)`
-Return the smallest value of all arguments.
+Return the smallest value from `list` of numbers or markers.
```cl
-(-min 0) ;; => 0
-(-min 1) ;; => 1
-(-min 1 2 3) ;; => 1
+(-min '(0)) ;; => 0
+(-min '(3 2 1)) ;; => 1
+(-min '(1 2 3)) ;; => 1
```
-### -min-by `(pred list)`
+### -min-by `(comparator list)`
-Call `pred` for each item in `list` and return item with smallest value.
+Take a comparison function `comparator` and a `list` and return
+the least element of the list by the comparison function.
+
+See also combinator `-on` which can transform the values before
+comparing them.
```cl
-(-min-by 'identity '()) ;; => nil
-(-min-by 'identity '(1)) ;; => 1
-(--min-by (cdr it) '((a . 1) (b . 2) (c . 3))) ;; => '(a . 1)
+(-min-by '> '(4 3 6 1)) ;; => 1
+(-min-by (-on '> 'length) '((1 2 3) (1) (1 2))) ;; => '(1)
+(-min-by (-on 'string-lessp 'int-to-string) '(2 100 22)) ;; => 22
```
-### -max `(x &rest xs)`
+### -max `(list)`
-Return the largest value of all arguments.
+Return the largest value from `list` of numbers or markers.
```cl
-(-max 0) ;; => 0
-(-max 1) ;; => 1
-(-max 1 2 3) ;; => 3
+(-max '(0)) ;; => 0
+(-max '(3 2 1)) ;; => 3
+(-max '(1 2 3)) ;; => 3
```
-### -max-by `(pred list)`
+### -max-by `(comparator list)`
+
+Take a comparison function `comparator` and a `list` and return
+the greatest element of the list by the comparison function.
-Call `pred` for each item in `list` and return item with largest value.
+See also combinator `-on` which can transform the values before
+comparing them.
```cl
-(-max-by 'identity '()) ;; => nil
-(-max-by 'identity '(1)) ;; => 1
-(--max-by (cdr it) '((a . 1) (b . 2) (c . 3))) ;; => '(c . 3)
+(-max-by '> '(4 3 6 1)) ;; => 6
+(-max-by (-on '> 'car) '((2 2 3) (3) (1 2))) ;; => '(3)
+(-max-by (-on '> 'string-to-int) '("1" "2" "3")) ;; => "3"
```
### -any? `(pred list)`
diff --git a/dash.el b/dash.el
index 51802bb..e70736d 100644
--- a/dash.el
+++ b/dash.el
@@ -906,47 +906,41 @@ Returns nil if N is less than 1."
"Return the product of LIST."
(apply '* list))
-(defun -min (x &rest xs)
- "Return the smallest value of all arguments."
- (apply 'min (cons x xs)))
-
-(defun -min-by (pred list)
- "Call PRED for each item in LIST and return item with smallest value."
- (let (min-item (min-value most-positive-fixnum))
- (-each
- list
- (lambda (item)
- (let ((item-value (funcall pred item)))
- (when (< item-value min-value)
- (setq min-value item-value)
- (setq min-item item)))))
- min-item))
+(defun -max (list)
+ "Return the largest value from LIST of numbers or markers."
+ (apply 'max list))
-(defmacro --min-by (form list)
- "Anaphoric form of `-min-by'."
- (declare (debug t))
- `(-min-by (lambda (it) ,form) ,list))
-
-(defun -max (x &rest xs)
- "Return the largest value of all arguments."
- (apply 'max (cons x xs)))
-
-(defun -max-by (pred list)
- "Call PRED for each item in LIST and return item with largest value."
- (let (max-item (max-value most-negative-fixnum))
- (-each
- list
- (lambda (item)
- (let ((item-value (funcall pred item)))
- (when (> item-value max-value)
- (setq max-value item-value)
- (setq max-item item)))))
- max-item))
+(defun -min (list)
+ "Return the smallest value from LIST of numbers or markers."
+ (apply 'min list))
+
+(defun -max-by (comparator list)
+ "Take a comparison function COMPARATOR and a LIST and return
+the greatest element of the list by the comparison function.
+
+See also combinator `-on' which can transform the values before
+comparing them."
+ (--reduce (if (funcall comparator it acc) it acc) list))
+
+(defun -min-by (comparator list)
+ "Take a comparison function COMPARATOR and a LIST and return
+the least element of the list by the comparison function.
+
+See also combinator `-on' which can transform the values before
+comparing them."
+ (--reduce (if (funcall comparator it acc) acc it) list))
(defmacro --max-by (form list)
- "Anaphoric form of `-max-by'."
- (declare (debug t))
- `(-max-by (lambda (it) ,form) ,list))
+ "Anaphoric version of `-max-by'.
+
+The items for the comparator form are exposed as \"it\" and \"other\"."
+ `(-max-by (lambda (it other) ,form) ,list))
+
+(defmacro --min-by (form list)
+ "Anaphoric version of `-min-by'.
+
+The items for the comparator form are exposed as \"it\" and \"other\"."
+ `(-min-by (lambda (it other) ,form) ,list))
(eval-after-load "lisp-mode"
'(progn
diff --git a/dev/examples.el b/dev/examples.el
index d71de97..8c750eb 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -110,26 +110,26 @@
(-product '(1 2 3)) => 6)
(defexamples -min
- (-min 0) => 0
- (-min 1) => 1
- (-min 1 2 3) => 1)
+ (-min '(0)) => 0
+ (-min '(3 2 1)) => 1
+ (-min '(1 2 3)) => 1)
(defexamples -min-by
- (-min-by 'identity '()) => nil
- (-min-by 'identity '(1)) => 1
- (--min-by (cdr it) '((a . 1) (b . 2) (c . 3))) => '(a . 1)
- (-min-by (lambda (x) (string-to-int x)) '("1" "2" "3")) => "1")
+ (-min-by '> '(4 3 6 1)) => 1
+ (-min-by (-on '> 'length) '((1 2 3) (1) (1 2))) => '(1)
+ (-min-by (-on 'string-lessp 'int-to-string) '(2 100 22)) => 22
+ (-min-by '< '(4 3 6 1)) => 6)
(defexamples -max
- (-max 0) => 0
- (-max 1) => 1
- (-max 1 2 3) => 3)
+ (-max '(0)) => 0
+ (-max '(3 2 1)) => 3
+ (-max '(1 2 3)) => 3)
(defexamples -max-by
- (-max-by 'identity '()) => nil
- (-max-by 'identity '(1)) => 1
- (--max-by (cdr it) '((a . 1) (b . 2) (c . 3))) => '(c . 3)
- (-max-by (lambda (x) (string-to-int x)) '("1" "2" "3")) => "3")
+ (-max-by '> '(4 3 6 1)) => 6
+ (-max-by (-on '> 'car) '((2 2 3) (3) (1 2))) => '(3)
+ (-max-by (-on '> 'string-to-int) '("1" "2" "3")) => "3"
+ (-max-by '< '(4 3 6 1)) => 1)
(defexamples -any?
(-any? 'even? '(1 2 3)) => t
- [elpa] externals/dash 824fcb3 207/439: Release 1.8.0, (continued)
- [elpa] externals/dash 824fcb3 207/439: Release 1.8.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0db2f5a 208/439: Fix second example for -last/first-item, Phillip Lord, 2015/08/04
- [elpa] externals/dash d1913c6 210/439: Merge pull request #41 from Wilfred/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 506401e 203/439: Merge pull request #39 from Fuco1/rotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash 37064b5 213/439: Include @Fuco1 in Authors/Copyright + fix README, Phillip Lord, 2015/08/04
- [elpa] externals/dash d6ca7d9 215/439: Fix E23 bug and re-enable E23 CI, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51c9f6b 212/439: Merge pull request #42 from Fuco1/functional, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9abae5e 211/439: Add combinators, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9a149bf 219/439: Add anaphoric example to --max-by and --min-by, Phillip Lord, 2015/08/04
- [elpa] externals/dash d374fcc 214/439: Revert "Add combinators", Phillip Lord, 2015/08/04
- [elpa] externals/dash 2a7fbdc 218/439: Update -max-by & friends,
Phillip Lord <=
- [elpa] externals/dash 6911514 221/439: Update docs too, Phillip Lord, 2015/08/04
- [elpa] externals/dash 86de963 217/439: Add docs about require for dash-functional, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4a144c6 220/439: Release 2.0.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9b0166a 224/439: Fix terminology, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6d0a8d6 225/439: Add Version and Package-Requires to files #45, Phillip Lord, 2015/08/04
- [elpa] externals/dash d365f22 222/439: Fix byte compile warnings, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1812a17 226/439: Complete dash and dash-functional split, Phillip Lord, 2015/08/04
- [elpa] externals/dash f1049f1 216/439: Add dash-functional back, now dependent on dash.el, Phillip Lord, 2015/08/04
- [elpa] externals/dash 39d20f2 228/439: Release 2.0.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 81e43ad 229/439: Add -union to list of highlighted symbols., Phillip Lord, 2015/08/04