Re: finding value nearest x

2014-03-05 Thread Yehonathan Sharvit
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 
> > 
> wrote:
> > On Sat, Sep 25, 2010 at 3:40 PM, Jules > 
> 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.


Re: finding value nearest x

2010-09-27 Thread Glen Rubin
ok, thx.  just trying to keep myself to a high standard while learning
this stuff ;)

On Sep 27, 11:12 am, Michael Gardner  wrote:
> On Sep 27, 2010, at 9:59 AM, Glen Rubin wrote:
>
> > yes correct.  but i can write a fn to determine the index of the
> > minimum distance in my new list?
>
> > that index applied to my original list will give me the value back.
> > and this still would involve fewer calculations i think.
>
> Do you have a particular reason to be concerned about performance here? Don't 
> worry about it unless profiling tells you it's a bottleneck for your program.
>
> And I doubt it will actually give better performance anyway, even with 
> min-key evaluating f more times than is necessary, because your f is so 
> inexpensive to calculate.

-- 
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


Re: finding value nearest x

2010-09-27 Thread Adam Burry
On Sep 25, 11:41 am, Glen Rubin  wrote:
> I have a list of numbers and I want to find the one that is closest to
> 136.  Is there an operator for performing this kind of operation or do
> I need to to do it algorithmically?

I think the normal way to do this is a k-d tree: 
http://en.wikipedia.org/wiki/Kd-tree

Adam

-- 
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


Re: finding value nearest x

2010-09-27 Thread Michael Gardner
On Sep 27, 2010, at 9:59 AM, Glen Rubin wrote:

> yes correct.  but i can write a fn to determine the index of the
> minimum distance in my new list?
> 
> that index applied to my original list will give me the value back.
> and this still would involve fewer calculations i think.

Do you have a particular reason to be concerned about performance here? Don't 
worry about it unless profiling tells you it's a bottleneck for your program.

And I doubt it will actually give better performance anyway, even with min-key 
evaluating f more times than is necessary, because your f is so inexpensive to 
calculate.

-- 
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


Re: finding value nearest x

2010-09-27 Thread Glen Rubin
yes correct.  but i can write a fn to determine the index of the
minimum distance in my new list?

that index applied to my original list will give me the value back.
and this still would involve fewer calculations i think.

On Sep 27, 10:50 am, Michael Gardner  wrote:
> On Sep 27, 2010, at 9:28 AM, Glen Rubin wrote:
>
> > It occurs to me that another way of doing this is to map a new list
> > and then use the min fn.  something like:
>
> > (apply min (map #(Math/abs (- % 136)) xs))
>
> > maybe this is better and involves less calculations?
>
> That gives you the minimum distance from 136, not the value itself. You can't 
> get back the original value afterwards either, because you don't know whether 
> to subtract or add the distance from 136.

-- 
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


Re: finding value nearest x

2010-09-27 Thread Michael Gardner
On Sep 27, 2010, at 9:28 AM, Glen Rubin wrote:

> It occurs to me that another way of doing this is to map a new list
> and then use the min fn.  something like:
> 
> (apply min (map #(Math/abs (- % 136)) xs))
> 
> maybe this is better and involves less calculations?

That gives you the minimum distance from 136, not the value itself. You can't 
get back the original value afterwards either, because you don't know whether 
to subtract or add the distance from 136.

-- 
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


Re: finding value nearest x

2010-09-27 Thread Glen Rubin
It occurs to me that another way of doing this is to map a new list
and then use the min fn.  something like:

(apply min (map #(Math/abs (- % 136)) xs))

maybe this is better and involves less calculations?



On Sep 25, 2:19 pm, Glen Rubin  wrote:
> min-key looks good!  thx guys!!!
>
> On Sep 25, 10:44 am, Nicolas Oury  wrote:
>
>
>
> > On Sat, Sep 25, 2010 at 3:40 PM, Jules  wrote:
> > > Maybe this: (min-key #(abs (- % 136)) xs)
>
> > Wouldn't that be (apply min-key #(abs (- % 136)) xs)?

-- 
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


Re: finding value nearest x

2010-09-25 Thread Glen Rubin
min-key looks good!  thx guys!!!

On Sep 25, 10:44 am, Nicolas Oury  wrote:
> On Sat, Sep 25, 2010 at 3:40 PM, Jules  wrote:
> > Maybe this: (min-key #(abs (- % 136)) xs)
>
> Wouldn't that be (apply min-key #(abs (- % 136)) xs)?

-- 
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


Re: finding value nearest x

2010-09-25 Thread Chouser
On Sat, Sep 25, 2010 at 10:44 AM, Nicolas Oury  wrote:
> On Sat, Sep 25, 2010 at 3:40 PM, Jules  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


Re: finding value nearest x

2010-09-25 Thread Eivind Magnus Hvidevold
(use 'clojure.contrib.math) ; for abs
(apply min-key #(abs (- 136 %)) [1 3 137 -137 135 0 50 75])

A recent thread in this group noted that min-key applies the function
multiple times and there's a better replacement. Also, if you're looking up
many such numbers you might want to sort and do binary search.

-- 
Eivind Magnus Hvidevold
Cell: +47 926 78 423



On Sat, Sep 25, 2010 at 3:41 PM, Glen Rubin  wrote:

> I have a list of numbers and I want to find the one that is closest to
> 136.  Is there an operator for performing this kind of operation or do
> I need to to do it algorithmically?
>
> thanks!
>
> --
> 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 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

Re: finding value nearest x

2010-09-25 Thread Jules
Yes, you're right.

On Sep 25, 4:44 pm, Nicolas Oury  wrote:
> On Sat, Sep 25, 2010 at 3:40 PM, Jules  wrote:
> > Maybe this: (min-key #(abs (- % 136)) xs)
>
> Wouldn't that be (apply min-key #(abs (- % 136)) xs)?

-- 
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


Re: finding value nearest x

2010-09-25 Thread Nicolas Oury
On Sat, Sep 25, 2010 at 3:40 PM, Jules  wrote:
> Maybe this: (min-key #(abs (- % 136)) xs)
>
Wouldn't that be (apply min-key #(abs (- % 136)) xs)?

-- 
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


Re: finding value nearest x

2010-09-25 Thread Jules
Maybe this: (min-key #(abs (- % 136)) xs)

On Sep 25, 3:41 pm, Glen Rubin  wrote:
> I have a list of numbers and I want to find the one that is closest to
> 136.  Is there an operator for performing this kind of operation or do
> I need to to do it algorithmically?
>
> thanks!

-- 
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


finding value nearest x

2010-09-25 Thread Glen Rubin
I have a list of numbers and I want to find the one that is closest to
136.  Is there an operator for performing this kind of operation or do
I need to to do it algorithmically?

thanks!

-- 
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