Update -max-by & friends

master
Fuco1 13 years ago committed by Magnar Sveen
parent 86de963a80
commit 2a7fbdc507
  1. 56
      README.md
  2. 70
      dash.el
  3. 28
      dev/examples.el

@ -29,10 +29,10 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-count](#-count-pred-list) `(pred list)` * [-count](#-count-pred-list) `(pred list)`
* [-sum](#-sum-list) `(list)` * [-sum](#-sum-list) `(list)`
* [-product](#-product-list) `(list)` * [-product](#-product-list) `(list)`
* [-min](#-min-x-rest-xs) `(x &rest xs)` * [-min](#-min-list) `(list)`
* [-min-by](#-min-by-pred-list) `(pred list)` * [-min-by](#-min-by-comparator-list) `(comparator list)`
* [-max](#-max-x-rest-xs) `(x &rest xs)` * [-max](#-max-list) `(list)`
* [-max-by](#-max-by-pred-list) `(pred list)` * [-max-by](#-max-by-comparator-list) `(comparator list)`
* [-any?](#-any-pred-list) `(pred list)` * [-any?](#-any-pred-list) `(pred list)`
* [-all?](#-all-pred-list) `(pred list)` * [-all?](#-all-pred-list) `(pred list)`
* [-none?](#-none-pred-list) `(pred list)` * [-none?](#-none-pred-list) `(pred list)`
@ -329,44 +329,52 @@ Return the product of `list`.
(-product '(1 2 3)) ;; => 6 (-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 ```cl
(-min 0) ;; => 0 (-min '(0)) ;; => 0
(-min 1) ;; => 1 (-min '(3 2 1)) ;; => 1
(-min 1 2 3) ;; => 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 ```cl
(-min-by 'identity '()) ;; => nil (-min-by '> '(4 3 6 1)) ;; => 1
(-min-by 'identity '(1)) ;; => 1 (-min-by (-on '> 'length) '((1 2 3) (1) (1 2))) ;; => '(1)
(--min-by (cdr it) '((a . 1) (b . 2) (c . 3))) ;; => '(a . 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 ```cl
(-max 0) ;; => 0 (-max '(0)) ;; => 0
(-max 1) ;; => 1 (-max '(3 2 1)) ;; => 3
(-max 1 2 3) ;; => 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 ```cl
(-max-by 'identity '()) ;; => nil (-max-by '> '(4 3 6 1)) ;; => 6
(-max-by 'identity '(1)) ;; => 1 (-max-by (-on '> 'car) '((2 2 3) (3) (1 2))) ;; => '(3)
(--max-by (cdr it) '((a . 1) (b . 2) (c . 3))) ;; => '(c . 3) (-max-by (-on '> 'string-to-int) '("1" "2" "3")) ;; => "3"
``` ```
### -any? `(pred list)` ### -any? `(pred list)`

@ -906,47 +906,41 @@ Returns nil if N is less than 1."
"Return the product of LIST." "Return the product of LIST."
(apply '* list)) (apply '* list))
(defun -min (x &rest xs) (defun -max (list)
"Return the smallest value of all arguments." "Return the largest value from LIST of numbers or markers."
(apply 'min (cons x xs))) (apply 'max list))
(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))
(defmacro --min-by (form list) (defun -min (list)
"Anaphoric form of `-min-by'." "Return the smallest value from LIST of numbers or markers."
(declare (debug t)) (apply 'min list))
`(-min-by (lambda (it) ,form) ,list))
(defun -max-by (comparator list)
(defun -max (x &rest xs) "Take a comparison function COMPARATOR and a LIST and return
"Return the largest value of all arguments." the greatest element of the list by the comparison function.
(apply 'max (cons x xs)))
See also combinator `-on' which can transform the values before
(defun -max-by (pred list) comparing them."
"Call PRED for each item in LIST and return item with largest value." (--reduce (if (funcall comparator it acc) it acc) list))
(let (max-item (max-value most-negative-fixnum))
(-each (defun -min-by (comparator list)
list "Take a comparison function COMPARATOR and a LIST and return
(lambda (item) the least element of the list by the comparison function.
(let ((item-value (funcall pred item)))
(when (> item-value max-value) See also combinator `-on' which can transform the values before
(setq max-value item-value) comparing them."
(setq max-item item))))) (--reduce (if (funcall comparator it acc) acc it) list))
max-item))
(defmacro --max-by (form list) (defmacro --max-by (form list)
"Anaphoric form of `-max-by'." "Anaphoric version of `-max-by'.
(declare (debug t))
`(-max-by (lambda (it) ,form) ,list)) 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" (eval-after-load "lisp-mode"
'(progn '(progn

@ -110,26 +110,26 @@
(-product '(1 2 3)) => 6) (-product '(1 2 3)) => 6)
(defexamples -min (defexamples -min
(-min 0) => 0 (-min '(0)) => 0
(-min 1) => 1 (-min '(3 2 1)) => 1
(-min 1 2 3) => 1) (-min '(1 2 3)) => 1)
(defexamples -min-by (defexamples -min-by
(-min-by 'identity '()) => nil (-min-by '> '(4 3 6 1)) => 1
(-min-by 'identity '(1)) => 1 (-min-by (-on '> 'length) '((1 2 3) (1) (1 2))) => '(1)
(--min-by (cdr it) '((a . 1) (b . 2) (c . 3))) => '(a . 1) (-min-by (-on 'string-lessp 'int-to-string) '(2 100 22)) => 22
(-min-by (lambda (x) (string-to-int x)) '("1" "2" "3")) => "1") (-min-by '< '(4 3 6 1)) => 6)
(defexamples -max (defexamples -max
(-max 0) => 0 (-max '(0)) => 0
(-max 1) => 1 (-max '(3 2 1)) => 3
(-max 1 2 3) => 3) (-max '(1 2 3)) => 3)
(defexamples -max-by (defexamples -max-by
(-max-by 'identity '()) => nil (-max-by '> '(4 3 6 1)) => 6
(-max-by 'identity '(1)) => 1 (-max-by (-on '> 'car) '((2 2 3) (3) (1 2))) => '(3)
(--max-by (cdr it) '((a . 1) (b . 2) (c . 3))) => '(c . 3) (-max-by (-on '> 'string-to-int) '("1" "2" "3")) => "3"
(-max-by (lambda (x) (string-to-int x)) '("1" "2" "3")) => "3") (-max-by '< '(4 3 6 1)) => 1)
(defexamples -any? (defexamples -any?
(-any? 'even? '(1 2 3)) => t (-any? 'even? '(1 2 3)) => t

Loading…
Cancel
Save