Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Add a "key" parameter to bisect* functions? (http://www.velocityreviews.com/forums/t952112-add-a-key-parameter-to-bisect-functions.html)

Giacomo Alzetta 09-12-2012 03:54 PM

Add a "key" parameter to bisect* functions?
 
I've just noticed that the bisect module lacks of the key parameter.

The documentation points to a recipe that could be used to handle a sorted collection, but I think it's an overkill if I want to bisect my sequence only once or twice with a key. Having something like `bisect(sequence, key=my_key)` would be much easier and would conform to the other operations such as max/min/sorted.

Is there some reason behind this lack?

Giacomo Alzetta 09-12-2012 04:03 PM

Re: Add a "key" parameter to bisect* functions?
 
Il giorno mercoledý 12 settembre 2012 17:54:31 UTC+2, Giacomo Alzetta ha scritto:
> I've just noticed that the bisect module lacks of the key parameter.
>
>
>
> The documentation points to a recipe that could be used to handle a sorted collection, but I think it's an overkill if I want to bisect my sequence only once or twice with a key. Having something like `bisect(sequence, key=my_key)` would be much easier and would conform to the other operations such as max/min/sorted.
>
>
>
> Is there some reason behind this lack?


Uhm, I've found this piece of documentation: http://docs.python.org/library/bisec...other-examples

Now, what it's stated there does not make sense for me:

1) Adding a key/reversed parameter wont decrease the speed of calls to bisect that does not use those parameters. At least a good implementation should guarantee that

2) Yes, providing a key means that either bisect should do some caching in order to speed multiple look-ups, or the keys have to be recomputed every time.
But, I think that using this as a reason to not provide this parameter is wrong.
It's up to the user to decide what has to be done to make code fast.
If he has to do multiple lookups then he should precompute them(but this istrue also for sort etc.), but if he just has to use this feature once thancomputing "on-the-fly" is simply perfect.

3) Also, the fact that you can bisect only in asceding order sounds strangeto me. It's not hard to bisect in both directions...

Probably it would be correct to document possible pitfalls of using the eventually added "key" and "reversed" parameters(such as multiple key evaluation etc.), but I can't see other cons to this.

Also, this change would be 100% backward compatible.

Miki Tebeka 09-12-2012 04:05 PM

Re: Add a "key" parameter to bisect* functions?
 
> I've just noticed that the bisect module lacks of the key parameter.
> ...
> Is there some reason behind this lack?

See full discussion at http://bugs.python.org/issue4356. Guido said it's going in, however there's no time frame for it.

Giacomo Alzetta 09-12-2012 04:22 PM

Re: Add a "key" parameter to bisect* functions?
 
Il giorno mercoledý 12 settembre 2012 18:05:10 UTC+2, Miki Tebeka ha scritto:
> > I've just noticed that the bisect module lacks of the key parameter.

>
> > ...

>
> > Is there some reason behind this lack?

>
> See full discussion at http://bugs.python.org/issue4356. Guido said it's going in, however there's no time frame for it.


Oh, nice. So eventually it'll be added.

Correct me if I'm wrong, but the reason for this absence is that people could think that doing repeated bisects using keys would be fast, even though keys has to be recomputed?

I think that could simply be fixed with a "Note:\Warning:" on the documentation.
So that you can create keys with caching or use precomputed keys when you have to do a lot of bisects.

Miki Tebeka 09-12-2012 05:24 PM

Re: Add a "key" parameter to bisect* functions?
 
> Correct me if I'm wrong, but the reason for this absence is that people could
> think that doing repeated bisects using keys would be fast, even though keys
> has to be recomputed?

I think the main point Raymond had was:
If we added key= to bisect, it would encourage bad design and steer
people after from better solutions.

But you need to ask him :)


All times are GMT. The time now is 07:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.