Details
-
Type:
Enhancement
-
Status:
Open
-
Priority:
Minor
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: Backlog
-
Component/s: None
-
Labels:None
-
Patch:Code and Test
Description
Reported by H.Duerer, Mar 13, 2009 max-key or min-key will evaluate (k value) multiple times for arguments if more than 2 arguments are passed. This is undesirable if k is expensive to calculate. Something like the code below would avoid these double calculations (at the price of generating more ephemeral garbage) (defn max-key "Returns the x for which (k x), a number, is greatest." ([k x] x) ([k x y] (if (> (k x) (k y)) x y)) ([k x y & more] (second (reduce (fn [x y] (if (> (first x) (first y)) x y)) (map #(vector (k %) %) (cons x (cons y more)))))))
Attachments
Activity
Andy Fingerhut
made changes -
| Field | Original Value | New Value |
|---|---|---|
| Attachment | clj-99-min-key-max-key-performance-v1.txt [ 11682 ] |
Andy Fingerhut
made changes -
| Patch | Code and Test [ 10002 ] | |
| Priority | Minor [ 4 ] | |
| Reporter | Andy Fingerhut [ jafingerhut ] |
Converted from http://www.assembla.com/spaces/clojure/tickets/99