Mark-Jason Dominus wrote:
> 
> > I'd think /perl/ should complain if your comparison function isn't
> > idempotent (if warnings on, of course).  If nothing else, it's probably an
> > indicator that you should be using that schwartz thang.
> 
> I have to agree with whoever followed up that this is a really dumb idea.  

Yahbut...  (See last paragraph, below).


> I explained how the /o in
>         /$foo/o
> represents a promise to Perl that $foo will never change, so Perl can
> skip the operation of checking to see if it has changed every time the
> match is performed.  Then there was a question from someone in the
> audience, asking if Perl would emit a warning if $foo changed.

That is not really an analogous situation.
If the comparison (or key extraction) function is not idempotent,
that is a much worse situation than simply one of degraded 
performance.  It means the result of sort() won't be (or at least
will not be guaranteed to be) sorted!


> Idempotency is not the important thing here.  The *important* property
> that the comparator needs, and the one that bad comparators usually
> lack is 
>         if my_compare(a,b) < 0, and
>            my_compare(b,c) < 0, then it should also be the case that
>            my_compare(a,c) < 0
> 
>         for all keys a, b, and c.

Hm. We could call that "relative idempotency", I suppose.


> An optimal sort function will not call the comparator if it
> already knows what the result should be!

But, aaui, it is not a question of the comparison function
being idempotent, but the key extraction function being so.

MJ's commentary on the relative idempotency of the comparison
function is valid.  But still, the real issues are the
idempotency and constness (pureness) of the key extraction
function.  Unless, that is, the results of the key extraction
function are memoized.  Given that they're *not* memoized,
non-idempotency of the function could be a problem.
However, as I said before, I think it is not worth the cost
(at least by default) for perl to assert idempotency.
And automatic memoizing could be turned on for any function
tagged with :constant (:function? :pure?).

-- 
John Porter

Give the braindead no head.

Reply via email to