On Sat, Jun 26, 2010 at 2:37 PM, P Kishor <[email protected]> wrote:

> On Sat, Jun 26, 2010 at 9:32 AM, Craig DeForest
> <[email protected]> wrote:
> > You are confusing direct assignment and computed assignment. "$b=$b*10"
> > creates a new PDL and puts it in the scalar named $b, dropping the old
> one
> > (and its dataflow connection to $a) on the floor. use computed assignment
> > (".=") instead.
> >
>
>
> Fair enough. But, add this to the list of inconsistencies.
>
> perldl> $a = sequence 5
> perldl> p $a
> [0 1 2 3 4]
>
> perldl> $b = $a->slice('1:3')
> perldl> $b = $b + 5
> perldl> p $b
> [6 7 8]
>
> However....
>
>
> perldl> p $a
> [0 1 2 3 4]
> perldl>
>
> perldl> $a = sequence 5
> perldl> p $a
> [0 1 2 3 4]
>
> perldl> $b = $a->slice('1:3')
> perldl> $b += 5
> perldl> p $b
> [6 7 8]
>
> perldl> p $a
> [0 6 7 8 4]
> perldl>
>

And sometimes, that's exactly what you want. :-)


> Similar to the sin($a) working, but int($a) not working. Yes, I know
> that int() is not a PDL function, but if sin(), cos(), etc. can be
> expected to work, if certain arithmetic operators are expected to
> work, then all of them should be expected to work.
>

Yes, I think we could overload the int operator. I could be wrong, but I
think it is as overloadable as sin or cos. This may be a good idea.


> Consistent interfaces are the hallmark of easy to learn, effective and
> powerful software. PDL is two out of three, but it would be nice for
> it to be 3 out of 3.
>

I agree. Unfortunately, with Perl's operator overloading, you can't get
there from here. It's a wart. We can't overload the assignment operator even
if we wanted to. We can overload +=, -=, .=, etc, but we can't overload the
= operator itself. Therefore,

--------%<--------
# Sequence returns a perl scalar that we hold in $a:
$a = sequence(10);
# slicing op returns a perl scalar that we hold in $b
$b = $a(2:5);
# addition op returns a (new) perl scalar that we store in $b:
$b = $b + 10;
--------%<--------

At this point, $b is no longer related to $a because the addition with $b
creates a new whole new piddle. New memory is allocated for it and
everything. On the other hand,

--------%<--------
# Sequence returns a perl scalar that we hold in $a:
$a = sequence(10);
# slicing op returns a perl scalar that we hold in $b
$b = $a(2:5);
# Overloaded += op changes contents of $b and, by dataflow, contents of $a
$b += 10;
--------%<--------

Remember that either way, $b holds the data you want. The difference is that
one case does data flow and the other doesn't. As a rule 'op=' operators,
like +=, -=, .=, etc, will do dataflow. Plain assignment operator does not
do dataflow. This should be emphasized somewhere in the documentation, and
I'm sure it is. However, it's not a beginner concept and it's hard to put it
somewhere that a graduating beginner will encounter with high certainty.

But, if you can find a way to effectively communicate all of these, that
would be awesome. :-D

David

-- 
Sent via my carrier pigeon.
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to