develooper Front page | perl.perl6.language | Postings from September 2002

reduce via ^ again

Thread Next
From:
John Williams
Date:
September 7, 2002 06:41
Subject:
reduce via ^ again
Message ID:
3D77EE65.6137EE2F@morinda.com
Apologies for trying to resuscitate this old horse, but a new idea
occurred to me.

Back in October I suggested that   $a ^+= @b   would act like reduce,
but in discussion
it was decided that it would act like length, by the interpretation:

    $a ^+= @b
    $a = $a ^+ @b
    $a = ($a, $a, $a, ...) ^+ @b
    $a = ($a+$b[0], $a+$b[1], ...)
    $a = (a list in scalar context )
    $a = length(@b)

I now pose the question: Is ^+= a "hyper assignment operator" or an
"assignment hyper operator"?
An "assignment hyper operator" would be interpreted as above, or with
two arrays as:

    @a ^+= @b
    @a = @a ^+ @b
    @a = ($a[0] + $b[0], $a[1] + $b[1], ... )

A "hyper assignment operator" would be interpreted like this (doing the
"hyper" part first):

    @a ^+= @b
    $a[0] += $b[0], $a[1] += $b[1], ...
    $a[0] = $a[0] + $b[0], $a[1] = $a[1] + $b[1], ...

With two arrays the method is different, but the end result is the
same.  (One might be more
efficient than the other, but I won't speculate on that here.)  But with
a scalar involved
the method and the result is different.  $a = length(@b) is the
"assignment hyper operator"
interpretation.  The "hyper assignment operator" interpretation looks
like this:

    $a ^+= @b
    ($a, $a, $a, ...) ^+= @b
    $a += $b[0], $a += $b[1], $a += $b[2], ...

....which is DWIM: $a = reduce(@b), assuming, of course that $a is
initially zero.

For @a = $b, the end result also appears to be the same.
"assignment hyper operator"
    @a ^+= $b
    @a = @a ^+ $b
    @a = @a ^+ ($b, $b, $b, ...)
    @a = ($a[0]+$b, $a[1]+$b, $a[2]+$b, ...)

"hyper assignment operator"
    @a ^+= $b
    @a  ^+= ($b, $b, $b, ...)
    $a[0] += $b, $a[1] += $b, $a[2] += $b, ...

~ John Williams



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About