Re: r26938 - docs/Perl6/Spec

2009-05-26 Thread Eirik Berg Hanssen
pugs-comm...@feather.perl6.nl writes:

>  statement, or if you want to attach multiple statements. you must either
>  use the curly form or surround the entire expression in brackets of some 
> sort:
>  
> -@primes = (do (do $_ if .prime) for 1..100);
> +@primes = do $_ if prime($_) for 1..100;

  I haven't been following much, but I'm pretty sure this example now
contradicts what it was once intended to illustrate, as the "entire
expression" is no longer "surrounded in" any kind of bracket.

  Is the whole you-must-either clause now obsolete?  Then I won't
bother to suggest that the Y should be upcased. ;-)


Eirik
-- 
Boston's Irreversible Law of Clutter: 
 In any household, junk accumulates to fill the space available 
 for its storage. 


Re: Question for Larry

2009-05-26 Thread TSa

HaloO,

John M. Dlugosz wrote:

Daniel Ruoso daniel-at-ruoso.com |Perl 6| wrote:

  %a := 1;

is an operation in the hash itself, not in that specific cell of the
hash.


This to me implies that postcircumfix:{'<','>'} returns some
assignment proxy that knows the hash. This is e.g. needed for
autovivification which happens in lvalue context but not in
rvalue context.


  


What?


Nothing in S02, S03 or S06 suggests such a usage.  := is used to alias a 
"name"  The expression on the left does not name a variable, but is an 
expression.


I would think that binding is the high level equivalent to pointer
assignment. Since %a is a proper lvalue binding can just assign
the lvalue from the right hand side to the left. Daniel's statement
above implies that the hash providing the lvalue location is involved
in this process. I would expect that

   my $x = 'foo';
   my %a := $x; # does \$x work as well?
   %a = 'from hash';
   say $x; # prints 'from hash'

works as indicated. Note that Perl 6 dropped the Perl 5 references.
%a = \$x is still valid syntax but stores a capture of $x which
is not modifiable, IIRC. Or would the code sequence above work also
with a capture instead of binding?


Regards, TSa.
--

"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan


Re: Meditations on a Loop

2009-05-26 Thread Patrick R. Michaud
On Mon, May 25, 2009 at 12:37:34PM -0700, yary wrote:
> That's an enjoyable and educational read, thanks!
> 
> There's one form under TMTOWTDI that I'd like to see, but can't figure
> out myself. It's the version analogous to this perl5 snippet-
> 
>   sub odd {$_ % 2}
>   say grep odd,0..6;
>
> -where the line that filters the list mentions no variables at all,
> and "$_" does its work behind the curtains.

How about...?

sub odd { ^$a % 2 }
say grep &odd, 0..6;

This gets us to within one character of the p5 version.  I think 
we also have the possibility of

subset odd where { $_ % 2 }
say grep odd, 0..6;

which is the same length as the p5 version, but afaict this 
last one doesn't seem to be working in Rakudo yet.

Pm


Unexpected behaviour with @foo.elems

2009-05-26 Thread Daniel Carrera

Hello,

The following construction doesn't do what a user might expect:

for 0...@foo.elems -> $k { do_something($k,@foo[$k]) }


Obviously, the intention is to step through every key/value in @foo. Buf 
@f...@foo.elems] does not exist. If @foo = (1,2,3); then @foo.elems is 
3, and @foo[3] is undefined.


Yes, I know that you could also have written:

for @foo.values -> $k { do_something($k,@foo[$k]) }


But I point out that the earlier code sample was given to me on IRC at 
#perl6. If one of the current developers can make that mistake, other 
users will too. I cannot say whether it makes sense to alter the 
language because of this. You are the language experts. I just wanted to 
raise a likely point of confusion among users.


Cheers,
Daniel.


Re: Meditations on a Loop

2009-05-26 Thread yary
On Tue, May 26, 2009 at 1:57 PM, Patrick R. Michaud  wrote:
> On Mon, May 25, 2009 at 12:37:34PM -0700, yary wrote:
> How about...?
>
>    sub odd { ^$a % 2 }
typo. "sub odd {$^a % 2}" works (caret goes between "$" and "a")

>    say grep &odd, 0..6;
nice. I need to learn the differences between calling a sub as "odd"
vs "&odd" in p6. Haven't gotten that far in the synopses yet.

> This gets us to within one character of the p5 version.  I think
> we also have the possibility of
>
>    subset odd where { $_ % 2 }
>    say grep odd, 0..6;
>
> which is the same length as the p5 version, but afaict this
> last one doesn't seem to be working in Rakudo yet.

Nope, doesn't seem to.

I was wondering why the perl5 example didn't work in p6- $_ is a
contextual variable, so why doesn't the body of "odd" get its $_ value
from grep in something like this:
sub odd_a { $_ % 2}
sub odd_b { $*_ % 2}
sub odd_c { $CONTEXT::_ % 2 }

say grep &odd_a, 0..6
(calls "say" 7 times with an uninitialized value. Same with &odd_b, &odd_c)


Re: Unexpected behaviour with @foo.elems

2009-05-26 Thread yary
I'm a relative beginner at perl6, but pretty good with perl5 (and C
and a few others), so I read
"for 0...@foo.elems"
as saying "Give me a list with one item longer then @foo", not "give
me the indexes of @foo". I can see users being tripped up by the old
problem of "we start counting at 0 and not at 1", which is more at the
root of what you're seeing. If perl6 started at 1 then you'd write

for 1...@foo.elems -> $k { do_something($k,@foo[$k]) } # wrong in this universe
for 0..(@foo.elems-1) -> $k { do_something($k,@foo[$k]) } # works

perl4-perl5.8 or so had a variable that let you change the starting
index for arrays, so you could actually make the above work. But then
everyone who'd re-arranged their brains to start counting at 0, and
written code that has a starting index of 0, would have problems.

>for @foo.values -> $k { do_something($k,@foo[$k]) }

try @foo.keys instead!

> But I point out that the earlier code sample was given to me on IRC at
> #perl6. If one of the current developers can make that mistake, other users
> will too. I cannot say whether it makes sense to alter the language because
> of this.

Changing the meaning of "elems" to mean anything other then the number
of elements seems likely to cause even more confusion!

I seem to recall one of the synopses discussing the replacement for
p5s "$#foo" to get the last index of foo, but I don't remember where.


Re: Unexpected behaviour with @foo.elems

2009-05-26 Thread John M. Dlugosz

Daniel Carrera daniel.carrera-at-theingots.org |Perl 6| wrote:

Hello,

The following construction doesn't do what a user might expect:

for 0...@foo.elems -> $k { do_something($k,@foo[$k]) }


Obviously, the intention is to step through every key/value in @foo. 
Buf @f...@foo.elems] does not exist. If @foo = (1,2,3); then 
@foo.elems is 3, and @foo[3] is undefined.


Yes, I know that you could also have written:

for @foo.values -> $k { do_something($k,@foo[$k]) }


But I point out that the earlier code sample was given to me on IRC at 
#perl6. If one of the current developers can make that mistake, other 
users will too. I cannot say whether it makes sense to alter the 
language because of this. You are the language experts. I just wanted 
to raise a likely point of confusion among users.


Cheers,
Daniel.



Write ^...@foo.elems as a shortcut of 0...@foo.elems, which is the
variation to exclude that endpoint if you would rather not write
0...@foo.elems-1.

--John



Re: Meditations on a Loop

2009-05-26 Thread John M. Dlugosz

yary not.com-at-gmail.com |Perl 6| wrote:

I was wondering why the perl5 example didn't work in p6- $_ is a
contextual variable,



 so why doesn't the body of "odd" get its $_ value
from grep in something like this:
sub odd_a { $_ % 2}
  
If you make it a formally declared "sub", then you have to set up your 
local $_ yourself.  If you wrote it as a bare block, then using $_ in 
that block would be enough to make it your parameter.


If you don't declare it, I'm not sure if you get a local one implicitly 
or if it makes a closure from the lexical scope containing the sub.  
Either way, it's not the dynamic scope.




sub odd_b { $*_ % 2}
  
Sayith S02, "Note that |$*_| will always see the |$_| in the current 
scope, not the caller's scope"

That implies that it is declared for you whether you use or not.


sub odd_c { $CONTEXT::_ % 2 }
  


I'd prefer CALLER to CONTEXT, but in this case they should be the same. 




say grep &odd_a, 0..6
(calls "say" 7 times with an uninitialized value. Same with &odd_b, &odd_c)

  

So the answer to "

why the perl5 example didn't work" is "more use of lexical scoping rules."

--John



r26940 - docs/Perl6/Spec

2009-05-26 Thread pugs-commits
Author: jdlugosz
Date: 2009-05-27 01:59:45 +0200 (Wed, 27 May 2009)
New Revision: 26940

Modified:
   docs/Perl6/Spec/S04-control.pod
Log:
[s04] replace example that no longer was applicable after previous edits; 
update old Array and List uses to Capture (and note that bare parens construct 
a Capture); add examples; clarify wording; fix typos.

Modified: docs/Perl6/Spec/S04-control.pod
===
--- docs/Perl6/Spec/S04-control.pod 2009-05-26 17:31:22 UTC (rev 26939)
+++ docs/Perl6/Spec/S04-control.pod 2009-05-26 23:59:45 UTC (rev 26940)
@@ -38,7 +38,8 @@
 $func = -> $a, $b { .print if $a eq $b }; # a "pointy" block
 $func = { .print if $^a eq $^b }  # placeholder arguments
 
-A bare closure without placeholder arguments that uses C<$_>
+A bare closure (except the block associated with a conditional statement)
+without placeholder arguments that uses C<$_>
 (either explicitly or implicitly) is treated as though C<$_> were a
 formal parameter:
 
@@ -200,8 +201,7 @@
 }
 
 If the final statement is a conditional which does not execute any
-branch, the return value is C in item context and C<()> in
-list context.
+branch, the return value is C.
 
 The C statement does not allow an C or C in Perl 6.
 
@@ -242,14 +242,14 @@
 
 Conditional statement modifiers work as in Perl 5.  So do the
 implicit conditionals implied by short-circuit operators.  Note though that
-the first expression within parens or brackets is parsed as a statement,
+the contents of parens or brackets is parsed as a semicolon-separated list of 
statements,
 so you can say:
 
 @x = 41, (42 if $answer), 43;
 
 and that is equivalent to:
 
-@x = 41, ($answer ?? 42 !! ()), 43
+@x = 41, ($answer ?? 42 !! Nil), 43
 
 =head1 Loop statements
 
@@ -515,10 +515,10 @@
 
 This construct only allows you to attach a single statement to the end
 of an expression.  If you want to continue the expression after the
-statement, or if you want to attach multiple statements. you must either
+statement, or if you want to attach multiple statements, you must either
 use the curly form or surround the entire expression in brackets of some sort:
 
-@primes = do $_ if prime($_) for 1..100;
+@primesquares = (do $_ if prime($_) for 1..100) [**] 2;
 
 Since a bare expression may be used as a statement, you may use C
 on an expression, but its only effect is to function as an unmatched
@@ -529,7 +529,7 @@
 the syntax inside brackets is a semicolon-separated list of statements,
 so the above can in fact be written:
 
-@primes = ($_ if prime($_) for 1..100);
+@primesquares = ($_ if prime($_) for 1..100) [**] 2;
 
 This basically gives us list comprehensions as rvalue expressions:
 
@@ -556,11 +556,28 @@
 Although a bare block occuring as a single statement is no longer
 a do-once loop, it still executes immediately as in Perl 5, as if it
 were immediately dereferenced with a C<.()> postfix, so within such a
-block C refers to the scope surrounding the block.
+block C refers to the scope surrounding the block.  But unlike
+an explicit call, C doesn't count it as a routine boundary.
 
 If you wish to return a closure from a function, you must use an
-explicit prefix such as C or C or C<< -> >>.
+explicit prefix such as C or C or C<< -> >>.  
 
+   sub f1
+   {
+   # lots of stuff ...
+   { say "I'm a closure." }
+   }
+   
+   my $x1= f1;  # fall-off return is result of the say, not the closure.
+   
+   sub f2
+   {
+   # lots of stuff ...
+   return { say "I'm a closure." }
+   }
+   
+   my $x2= f2;  # returns a Block object.
+
 Use of a placeholder parameter in statement-level blocks triggers a
 syntax error, because the parameter is not out front where it can be
 seen.  However, it's not an error when prefixed by a C, or when
@@ -619,8 +636,8 @@
 my $x = take $_, $_ * 10;   # $() context for individual capture
 push @y, $x;
 }
-# @x returns 1,10,2,20
-# @y returns [1,10],[2,20]
+# @x contains 4 Ints:  1,10,2,20
+# @y contains 2 Captures: (1,10),(2,20)
 
 Likewise, we can just remember the gather's result by binding and
 later coerce it:
@@ -628,9 +645,9 @@
 $c := gather for 1..2 {
 take $_, $_ * 10;
 }
-# @$c returns 1,10,2,20
-# @@$c returns [1,10],[2,20]
-# $$c returns [[1,10],[2,20]]
+# @$c produces 1,10,2,20 -- flatten fully into a list of Ints.
+# @@$c produces (1,10),(2,20) -- list of Captures, a 2-D list.
+# $$c produces ((1,10),(2,20)) -- the saved Capture itself as one item in 
item context.
 
 Note that the C itself is in void context in this example because
 the C loop is in void context.
@@ -659,6 +676,10 @@
 All other names (including implicit names of operators) are looked up
 in the lexical scope of the caller when we actually know who the caller
 is at run time.  (Note 

Re: r26938 - docs/Perl6/Spec

2009-05-26 Thread John M. Dlugosz

I fixed that today... will check in in a few hours.
It's harder to come up with a new example than to update syntax. :)

--John



Eirik Berg Hanssen Eirik-Berg.Hanssen-at-allverden.no |Perl 6| wrote:

pugs-comm...@feather.perl6.nl writes:

  

 statement, or if you want to attach multiple statements. you must either
 use the curly form or surround the entire expression in brackets of some sort:
 
-@primes = (do (do $_ if .prime) for 1..100);

+@primes = do $_ if prime($_) for 1..100;



  I haven't been following much, but I'm pretty sure this example now
contradicts what it was once intended to illustrate, as the "entire
expression" is no longer "surrounded in" any kind of bracket.

  Is the whole you-must-either clause now obsolete?  Then I won't
bother to suggest that the Y should be upcased. ;-)


Eirik
  




Re: Unexpected behaviour with @foo.elems

2009-05-26 Thread Patrick R. Michaud
On Tue, May 26, 2009 at 06:43:40PM -0500, John M. Dlugosz wrote:
> Daniel Carrera daniel.carrera-at-theingots.org |Perl 6| wrote:
>> The following construction doesn't do what a user might expect:
>>
>> for 0...@foo.elems -> $k { do_something($k,@foo[$k]) }
>
> Write ^...@foo.elems as a shortcut of 0...@foo.elems, which is the
> variation to exclude that endpoint if you would rather not write
> 0...@foo.elems-1.

An even cleaner shortcut might be to use ^...@foo instead of ^...@foo.elems:

for ^...@foo -> $k { do_something($k, @foo[$k]) }

Somewhat clearer could be:

for @foo.keys -> $k { do_something($k, @foo[$k]) }

And some may prefer:

for @foo.kv -> $k, $v { do_something($k, $v) }

I think the anti-pattern of "0...@foo.elems" (or its incorrect
form "0...@foo.elems") should probably disappear in favor of
the above forms instead.

Pm


Continuations

2009-05-26 Thread Jon Lang
>From S09, under Junctions:

"The exact semantics of autothreading with respect to control
structures are subject to change over time; it is therefore erroneous
to pass junctions to any control construct that is not implemented via
as a normal single or multi dispatch. In particular, threading
junctions through conditionals correctly could involve continuations,
which are almost but not quite mandated in Perl 6.0.0."

What is a continuation?

-- 
Jonathan "Dataweaver" Lang


Re: Continuations

2009-05-26 Thread John M. Dlugosz

Jon Lang dataweaver-at-gmail.com |Perl 6| wrote:

>From S09, under Junctions:

"The exact semantics of autothreading with respect to control
structures are subject to change over time; it is therefore erroneous
to pass junctions to any control construct that is not implemented via
as a normal single or multi dispatch. In particular, threading
junctions through conditionals correctly could involve continuations,
which are almost but not quite mandated in Perl 6.0.0."

What is a continuation?

  



Early on, Perl 6 discussion featured a lot on Continuations.  Now, I 
don't see it anywhere at all, and believe that the general form is not 
required, by design.  That is, "not mandated".  It's a computer science 
concept that generalizes *all* forms of flow control including 
exceptions, co-routines, etc.  The long-jump or exception is a more 
normal case of returning to something that is still in context, but 
imagine if you could go both ways:  bookmark something in the code, like 
making a closure but for the complete calling stack of activation 
complexes, and then jump back to it later.


Re: Meditations on a Loop

2009-05-26 Thread Patrick R. Michaud
On Tue, May 26, 2009 at 04:10:45PM -0700, yary wrote:
> On Tue, May 26, 2009 at 1:57 PM, Patrick R. Michaud  
> wrote:
> > On Mon, May 25, 2009 at 12:37:34PM -0700, yary wrote:
> > How about...?
> >
> >    sub odd { ^$a % 2 }
> typo. "sub odd {$^a % 2}" works (caret goes between "$" and "a")

Correct, that's a typo.

> >    say grep &odd, 0..6;
> nice. I need to learn the differences between calling a sub as "odd"
> vs "&odd" in p6. Haven't gotten that far in the synopses yet.

In p6, one always uses the sigil to refer to the sub itself; the bare
identifier form invokes it.

> I was wondering why the perl5 example didn't work in p6- $_ is a
> contextual variable, so why doesn't the body of "odd" get its $_ value
> from grep in something like this:
> sub odd_a { $_ % 2}

This is really equivalent to

sub odd_a(*...@_, *%_) { $_ % 2 }

Every Routine gets its own $_ that isn't inherited from the outer 
lexical context (and is initialized to undef).  

Beyond that, what is really happening with

say grep &odd_a, 0..6

is that grep is performing a smart-match of each value of 0..6 
with &odd_a, and smart-matching a value to a Code object invokes
the Code object with the value as an argument.

Pm


Re: Continuations

2009-05-26 Thread Jon Lang
On Tue, May 26, 2009 at 8:05 PM, John M. Dlugosz
<2nb81l...@sneakemail.com> wrote:
> Jon Lang dataweaver-at-gmail.com |Perl 6| wrote:
>>
>> >From S09, under Junctions:
>>
>> "The exact semantics of autothreading with respect to control
>> structures are subject to change over time; it is therefore erroneous
>> to pass junctions to any control construct that is not implemented via
>> as a normal single or multi dispatch. In particular, threading
>> junctions through conditionals correctly could involve continuations,
>> which are almost but not quite mandated in Perl 6.0.0."
>>
>> What is a continuation?
>>
>>
>
> 
>
> Early on, Perl 6 discussion featured a lot on Continuations.  Now, I don't
> see it anywhere at all, and believe that the general form is not required,
> by design.  That is, "not mandated".  It's a computer science concept that
> generalizes *all* forms of flow control including exceptions, co-routines,
> etc.  The long-jump or exception is a more normal case of returning to
> something that is still in context, but imagine if you could go both ways:
>  bookmark something in the code, like making a closure but for the complete
> calling stack of activation complexes, and then jump back to it later.
>

OK; that's about as clear as mud.  But I think I've got a rough idea
about what you're talking about.  Next question: what does that have
to do with junctions?

-- 
Jonathan "Dataweaver" Lang