Chaim Frenkel wrote:
>
>TC> And even in those rare places where they do, to use them as such
>TC> is gratuitously counter-efficient.
>
>Why? How is
>
>       @main_list = ....;
>       @more_stuf = ....;
>
>       @just_to_do_a_job{@main_list} = ();
>       @just_to_do_a_job{@more_stuff} = ();
>
>       @main_list = keys %just_to_do_a_job;
>
>more or less efficient than
>
>       @main_list union= @more_stuff;
>
>Both have to go through the same amount of work ("work is conserved")
>but one is more efficient in terms of the user's brainpower.

Why not simply do

    @main_list = (@main_list, @more_stuf);  ?

What? You say you don't want duplicates in the resulting list?
Why didn't you say so?

So how is the hypothetical union operator going to check for dupes?
Loop over each element of the first array, checking whether it is
contained somewhere in the second array (by looping over it)?
Dude, O(m*n) ain't good.

Or perhaps you think that Perl should do for you what you're not
willing to do for yourself: it should create two temporary hashes
to do the collision detection. A lookup table is a great way to 
find collisions, right? And a hash is a great way to implement a
lookup table, right? Yes! So Perl should internally create hashes
to do your union operator. And intersection operator. And whatever.

But don't you think creating and destroying all those temporary 
hashes is going to be a waste of your computer's time? I mean, I
know hardware is cheap and all, but....

Why not create a permanent lookup table of all the elements of each
of your sets. Wouldn't that be a good optimization? Sure! So instead
of:

    @first_list  = qw/one two three/;
    @second_list = qw/two four six/;

create hashes:

    %first_set  = map {$_=>()} qw/one two three/;
    %second_set = map {$_=>()} qw/two four six/;

Don't like that ugly syntax? Can't remember it? Here's a free sub for
you:

    sub create_set { map {$_=>()} @_ }


"But," I hear you wail, "I like using arrays for sets! It makes sense
to me!". Tell me, Chaim, how DO you test for membership in your array
world? grep? 

    if (grep {$_ eq 'three'} @first_list)

Talk about ugly. And slow: O(n/2). What's wrong with:

    if (exists $first_set{three})

Simple, elegant, fast. O(1), more or less.

You think hash functions are ugly? Then wrap them in functions so
you don't have to look at them. 

How *do* you propose to implement union and intersection with arrays,
internally in Perl? Any idea? Double-looping over the arrays is 
horribly slow. Forcing Perl to create and destroy lookup tables each
time is wasteful. Did you have a better idea?

If you want Perl to have core support for widgets like union and
intersection, that's one thing. Make a case for it. But you have
repeatedly tried to make a case for arrays as set representations
and you have not made a good case yet. Your least-bad argument has
been that arrays "make more sense to you" than hashes. Does execution
time mean nothing to you?

 ----------------------------------------------------------------------
 Eric J. Roode,  [EMAIL PROTECTED]           print  scalar  reverse  sort
 Senior Software Engineer                'tona ', 'reh', 'ekca', 'lre',
 Myxa Corporation                        '.r', 'h ', 'uj', 'p ', 'ts';

Reply via email to