Re: given vs for
Paul Fenwick pjf at perltraining.com.au writes: for ($foo) { when ($_ 500) { ++$_ } when ($_ 1000) { --$_ } default { say Just right $_ } } Ahh... that's exactly what I was looking for. Thanks. Makes you wonder why the 'given' keyword was added, when for/when is so close... I'd assume 'given' provides scalar context onto its argument, 'for' obviously provides list context. But I guess the main difference is that 'for' is associated with iteration, and IMHO it feels unnatural to iterate over one item. -- Moritz Lenz http://moritz.faui2k3.org/ | http://perl-6.de/ signature.asc Description: OpenPGP digital signature
Re: given vs for
In a message dated Fri, 25 Apr 2008, Moritz Lenz writes: Paul Fenwick pjf at perltraining.com.au writes: for ($foo) { when ($_ 500) { ++$_ } when ($_ 1000) { --$_ } default { say Just right $_ } } Ahh... that's exactly what I was looking for. Thanks. Makes you wonder why the 'given' keyword was added, when for/when is so close... I'd assume 'given' provides scalar context onto its argument, 'for' obviously provides list context. But I guess the main difference is that 'for' is associated with iteration, and IMHO it feels unnatural to iterate over one item. In 5.10, given seems to copy its argument, whereas for aliases it. (I haven't looked at the code; maybe it's COW-ing it.) If you add a Csay Value is now $foo; to the end of the below program, and then change Cfor to Cgiven and run the program with values of $foo less than 500 or greater than 1000, you'll see the difference: with Cfor, the value remains changed after the block, with Cgiven it is not. I do not believe this distinction is true in Perl 6, is it? Trey
Re: given vs for
AIUI, this is the difference: given (@foo) { # this code runs exactly once, topic is @foo } vs for (@foo) { # this code runs once per item in @foo, topic # is @foo[0], then @foo[1], etc. } So eseentially, given (@foo) means the same as Perl5 for ([EMAIL PROTECTED])
Re: given vs for
Moritz Lenz moritz-at-casella.verplant.org |Perl 6| wrote: Paul Fenwick pjf at perltraining.com.au writes: for ($foo) { when ($_ 500) { ++$_ } when ($_ 1000) { --$_ } default { say Just right $_ } } Ahh... that's exactly what I was looking for. Thanks. Makes you wonder why the 'given' keyword was added, when for/when is so close... I'd assume 'given' provides scalar context onto its argument, 'for' obviously provides list context. But I guess the main difference is that 'for' is associated with iteration, and IMHO it feels unnatural to iterate over one item. given and when are not dedicated as a matched set as in select/case or such in other languages. They are independent features. 'given' sets a topic if you just need to do that. If you are in a loop or otherwise like to use the $_ variable, you don't need to. If you lean away from using the default variable and like to name things, then 'given' comes in handy for 'when' and for something like Pascal's 'with' statement for accessing a bunch of methods in one object. 'for' takes a list as its argument. The parens don't add anything, they are just for old times' sake. The writeup in S04 is not detailed, but if the control argument is equivalent to *$@, then you specified a list of one item. But the resulting $_ is a single item. --John
Re: given vs for
Trey Harris trey-at-lopsa.org |Perl 6| wrote: In 5.10, given seems to copy its argument, whereas for aliases it. (I haven't looked at the code; maybe it's COW-ing it.) If you add a Csay Value is now $foo; to the end of the below program, and then change Cfor to Cgiven and run the program with values of $foo less than 500 or greater than 1000, you'll see the difference: with Cfor, the value remains changed after the block, with Cgiven it is not. I do not believe this distinction is true in Perl 6, is it? Trey Correct. In S04, If you rely on C$_ as the implicit parameter to a block, then C$_ is considered read/write by default. That is, the construct: for @foo {...} is actually short for: for @foo - $_ {...} so you can modify the current list element in that case. Are you saying that Perl 5.10 has given/when ? Or is that the CPAN module that basically rewrites the entire file? --John
Re: given vs for
On Fri, Apr 25, 2008 at 10:39 AM, John M. Dlugosz [EMAIL PROTECTED] wrote: Are you saying that Perl 5.10 has given/when ? Yes. Perl 5.10 has several Perl 6 features back-ported into it, available via the use feature pragma: say (enables the say() built-in), state (enables state vars), switch (enables given/when). Or is that the CPAN module that basically rewrites the entire file? No. -- Mark J. Reed [EMAIL PROTECTED]
Re: given vs for
HaloO, John M. Dlugosz wrote: for @foo {...} is actually short for: for @foo - $_ {...} Ups, I missed that one. Do we also have the fill-me idiom for @foo - $_ {...} And again the question if this is the same as for @foo - $_ is ref {...} Regards, TSa. -- The unavoidable price of reliability is simplicity -- C.A.R. Hoare
Re: given vs for
Mark J. Reed wrote: So eseentially, given (@foo) means the same as Perl5 for ([EMAIL PROTECTED]) Just wondering: should given @foo {...} alias to $_, or @_?
Re: given vs for
Dave Whipp writes: Mark J. Reed wrote: So eseentially, given (@foo) means the same as Perl5 for ([EMAIL PROTECTED]) Just wondering: should given @foo {...} alias to $_, or @_? I'd expect it to alias to C$_, on the grounds that everything always aliases to C$_. What's the argument for it being C@_? Smylers
Re: given vs for
Smylers wrote: Dave Whipp writes: So eseentially, given (@foo) means the same as Perl5 for ([EMAIL PROTECTED]) Just wondering: should given @foo {...} alias to $_, or @_? I'd expect it to alias to C$_, on the grounds that everything always aliases to C$_. What's the argument for it being C@_? Yes, my expectation would be C$_, too. But then I started thinking that it might be nice to preserve the sigil; and that binding to C@_ is what subs do, so it's not entirely unprecedented. But yes, the use-case of given/when is based on the idea of creating a scalar alias to the collection.
Re: given vs for
The topic should always be $_ unless explicitly requested differently via the arrow. Now in the case of for, it might be nice if @_ bound to the entire collection being iterated over (if any)...
Re: given vs for
TSa Thomas.Sandlass-at-barco.com |Perl 6| wrote: HaloO, John M. Dlugosz wrote: for @foo {...} is actually short for: for @foo - $_ {...} Ups, I missed that one. Do we also have the fill-me idiom for @foo - $_ {...} No. There is no concept of output parameters. And again the question if this is the same as for @foo - $_ is ref {...} is rw, not is ref. But yes, the double-ended arrow is the same as making rw the default on all the parameters. Regards, TSa.
Re: given vs for
Dave Whipp dave-at-whipp.name |Perl 6| wrote: Mark J. Reed wrote: So eseentially, given (@foo) means the same as Perl5 for ([EMAIL PROTECTED]) Just wondering: should given @foo {...} alias to $_, or @_? $_. It will contain the whole list as one item, like what Perl 5 does with [EMAIL PROTECTED] given @foo { when .length 5 { say That's a long list } when .length == Inf { say That's a very long list } when .WHAT ~~ Range { say That's an iterator } }
Re: given vs for
On Fri, Apr 25, 2008 at 01:19:27PM -0500, John M. Dlugosz wrote: given @foo { when .length 5 { say That's a long list } when .length == Inf { say That's a very long list } when .WHAT ~~ Range { say That's an iterator } } Erm, .length is dead, and .WHAT just smells that way. given @foo { when .elems 5 { say That's a long list } when .elems == Inf { say That's a very long list } when Range { say That's an iterator } } Larry
Re: given vs for
Mark J. Reed wrote: The topic should always be $_ unless explicitly requested differently via the arrow. Now in the case of for, it might be nice if @_ bound to the entire collection being iterated over (if any)... As a perl5-ism: sub foo { say @_; } ... given (@bar) { when ... { foo } } Does perl6 still have some implicit mechanism to say call sub using current arglist? (No, I'm not arguing to support any of this: just asking the questions)
Re: given vs for
On Fri, Apr 25, 2008 at 01:05:37PM -0700, Dave Whipp wrote: As a perl5-ism: sub foo { say @_; } ... given (@bar) { when ... { foo } } Does perl6 still have some implicit mechanism to say call sub using current arglist? Yes, you can do it implicitly with one of callsame, callwith, nextsame and nextwith (S06 and S12), depending on whether you want to specify what to call next or just rely on the dispatcher to pick the next one, and on whether you want to pass the original arguments or some modified arguments. You can also do it explicitly with a Capture declaration in the signature and the prefix:| capture interpolator in an argument list.. However, foo doesn't mean what it means in Perl 5. It's just the function as a noun rather than a verb. Larry
Re: given vs for
To loop back to my earlier question: In Perl 5.10: use strict; use warnings; use feature qw(switch say); my $foo = 10; for ($foo) { when ($foo 50) { $_++ } } say for: $foo; $foo = 10; given ($foo) { when ($foo 50) { $_++ } } say given: $foo; Prints: for: 11 given: 10 In Perl 6, that same code (minus the three Cuse lines) should print: for: 11 given: 11 Correct? Trey
Re: given vs for
Dave Whipp dave-at-whipp.name |Perl 6| wrote: Does perl6 still have some implicit mechanism to say call sub using current arglist? (No, I'm not arguing to support any of this: just asking the questions) Yes. You can use 'callsame' and it knows the current argument list. You can get at it explicitly if you declare it like this: sub foo (|$arglist) { ... Now $arglist is an object of type Capture, which would be getting into a lot more than a simple reply. It's not just a simple @_ array, but handles named, optional, slice, etc. =head2 Argument list binding The underlying CCapture object may be bound to a single scalar parameter marked with a C|. sub bar ($a,$b,$c,:$mice) { say $mice } sub foo (|$args) { say $args.perl; bar.callwith(|$args); } This prints: foo 1,2,3,:miceblind; # says \(1,2,3,:miceblind) then blind See http://svn.perl.org/perl6/doc/trunk/design/syn/S06.pod for more information.
Re: given vs for
Larry Wall larry-at-wall.org |Perl 6| wrote: However, foo doesn't mean what it means in Perl 5. It's just the function as a noun rather than a verb. Larry A gerund.