In the sorted-set solution, you forgot to handle the case where all the values in the sorted-set are greater than 136...
On Saturday, 25 September 2010 17:55:33 UTC+2, Chouser wrote: > > On Sat, Sep 25, 2010 at 10:44 AM, Nicolas Oury > <nicola...@gmail.com<javascript:>> > wrote: > > On Sat, Sep 25, 2010 at 3:40 PM, Jules <jules...@gmail.com <javascript:>> > wrote: > >> Maybe this: (min-key #(abs (- % 136)) xs) > >> > > Wouldn't that be (apply min-key #(abs (- % 136)) xs)? > > Where's your 'abs' function coming from? This works for me: > > (apply min-key #(Math/abs (- % 136)) xs) > > Or if you want something slower when building the collection but > faster when looking it up, you can use a sorted set: > > (let [ss (sorted-set 1 2 134 139 4), > asc (subseq ss >= 136), > desc (rsubseq ss <= 136)] > (min-key #(Math/abs (- % 136)) (first asc) (first desc))) > > That's O(log n) instead the simpler (apply min-key ...) which is O(n). > > --Chouser > http://joyofclojure.com/ > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.