On Sat, Jun 26, 2010 at 7:02 PM, P Kishor <[email protected]> wrote:
> On Sat, Jun 26, 2010 at 6:46 PM, David Mertens <[email protected]> > wrote: > > > > > > On Sat, Jun 26, 2010 at 6:18 PM, P Kishor <[email protected]> wrote: > >> > >> On Sat, Jun 26, 2010 at 6:08 PM, David Mertens < > [email protected]> > >> wrote: > >> > On Sat, Jun 26, 2010 at 3:47 PM, P Kishor <[email protected]> > wrote: > >> >> > >> >> <snip> > >> >> On a related note... if I have my own function > >> >> > >> >> sub foo { > >> >> my ($a, $b, $c, $piddle) = @_; > >> >> do something to every element in $piddle based on $a, $b, $c > >> >> } > >> >> > >> >> I want to invoke it as a method call on my piddle $p, like so > >> >> > >> >> $p->foo($a, $b, $c) > >> >> > >> >> How do I construct that? > >> > > >> > This is question about writing object-oriented Perl. This is a whole > new > >> > can > >> > of worms, but here are the basics. > >> > > >> > First, the method must take the piddle as its first argument. Second, > >> > the > >> > subroutine must be defined in the PDL package. Using these two ideas, > >> > you > >> > would instead have this: > >> > > >> > sub PDL::foo { > >> > my ($piddle, $a, $b, $c) = @_; > >> > # do something here > >> > } > >> > > >> > Defining the cod as such will allow you to write this code: > >> > > >> > @results = $piddle->foo($a, $b, $c); > >> > > >> > You may want foo to be both a function and a method. That is, you may > >> > want > >> > > >> > foo($piddle, $a, $b, $c) > >> > > >> > and > >> > > >> > $piddle->foo($a, $b, $c) > >> > >> Thanks David. This is exactly what I wanted to know. I know how to > >> create an Perl class and write OO code, but I wasn't sure if > >> PDL-wizards had already provide some funky shortcut for doing so. > >> > >> As it is, PDL continues to blow my mind. I frankly don't understand > >> why PDL is not a part of core Perl (besides being a pain-in-ass to > >> install), as I can't really imagine working with normal arrays ever > >> again, if I can help it. > >> > >> Anyway, yes, now that I know that I have to write the OO code myself, > >> but once I write my method, PDL will automatically apply it to every > >> element in my piddle, that is do-able magic! > > > > No no no! I misunderstood you. You were asking how to write a function > that > > would automatically thread. That is not trivial. At the moment we don't > have > > a way to auto-thread a Perl function. The only way to make a Perl > function > > 'threadable' is to make sure of all its operations use threaded > functions. > > To write a (PDL-)threaded function, you have to use PDL::PP. > > > > Sorry for the confusion. > > > > > Not really. Consider the following > > perldl> $a = sequence 5 > perldl> p $a > [0 1 2 3 4] > > perldl> $b = sub { if ( $_[1] eq 'two' ) { return $_[0] * 2 } else { > return $_[0] * 5 } } > perldl> $c = $a->$b > perldl> p $c > [0 5 10 15 20] > > perldl> $d = $a->$b('two') > perldl> p $d > [0 2 4 6 8] > perldl> > > > Maybe I misstated my own question earlier, but the above is exactly > what I want, and it seems to work well. > > Puneet. > Well, if you're happy, then all's well. :-) -- Sent via my carrier pigeon.
_______________________________________________ Perldl mailing list [email protected] http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
