Skip to content

Commit 379e364

Browse files
committed
Spacing, Perl 6 vs Perl 6 (nbsp)
1 parent 2715812 commit 379e364

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

doc/Language/py-nutshell.pod6

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ contained in curly braces are interpolated.
4646
Perl 6
4747
4848
my $planet = 'earth';
49-
say "Hello, $planet"; # Hello, earth
50-
say 'Hello, $planet'; # Hello, $planet
49+
say "Hello, $planet"; # Hello, earth
5150
say "Hello, planet number { 1 + 2 }"; # Hello, planet number 3
51+
say 'I have $2'; # I have $2
5252
5353
=head2 Statement Separators
5454
@@ -64,39 +64,41 @@ The semicolon may be omitted if it is the last statement
6464
of a block. The semicolon may also be omitted if there
6565
is a closing curly brace followed by a newline.
6666
67-
Python:
67+
Python
6868
6969
print 1 + 2 + \
7070
3 + 4
7171
print ( 1 +
7272
2 )
7373
74-
Perl 6:
74+
Perl 6
7575
7676
say 1 + 2 +
7777
3 + 4;
7878
if True { say 1 + 2 }
7979
8080
=head2 Blocks
8181
82-
In Python indentation is used to indicate a block. Perl 6
82+
In Python, indentation is used to indicate a block. Perl 6
8383
uses curly braces.
8484
85-
Python:
85+
Python
86+
8687
if 1 == 2:
8788
print "Wait, what?"
8889
else:
8990
print "1 is not 2."
9091
91-
Perl 6:
92+
Perl 6
93+
9294
if 1 == 2 {
9395
say "Wait, what?"
9496
} else {
9597
say "1 is not 2."
9698
}
9799
98-
Parentheses are optional in both languages in expressions in conditionals,
99-
as shown above.
100+
Parentheses are optional in both languages in expressions in
101+
conditionals, as shown above.
100102
101103
=head2 Variables
102104
@@ -119,8 +121,9 @@ hold arrays, and variables starting with a C<%> hold a hash (dict).
119121
Immutable variables can be sigil-less, if they are declared with a C<\>.
120122
121123
Python
124+
122125
s = 10
123-
l = [1,2,3]
126+
l = [1, 2, 3]
124127
d = { a : 12, b : 99 }
125128
126129
print s
@@ -129,6 +132,7 @@ Python
129132
# 10, 2, 12
130133
131134
Perl 6
135+
132136
my $s = 10;
133137
my @l = 1, 2, 3;
134138
my %d = a => 12, b => 99;
@@ -144,18 +148,20 @@ Perl 6
144148
=head2 Scope
145149
146150
In Python, functions and classes create a new scope, but no other
147-
block constructor (e.g. loops, conditionals) creates a scope. In Python 2,
148-
list comprehensions do not create a new scope, but in Python 3, they do.
151+
block constructor (e.g. loops, conditionals) creates a scope. In
152+
Python 2, list comprehensions do not create a new scope, but in Python 3, they do.
149153
150154
In Perl 6, every block creates a lexical scope.
151155
152156
Python
157+
153158
if True:
154159
x = 10
155160
print x
156161
# x is now 10
157162
158163
Perl 6
164+
159165
if True {
160166
my $x = 10
161167
}
@@ -170,13 +176,15 @@ Perl 6
170176
# ok, x is 10
171177
172178
Python
179+
173180
x = 10
174181
for x in 1, 2, 3:
175182
pass
176183
print x
177184
# x is 3
178185
179186
Perl 6
187+
180188
my \x = 10;
181189
for 1, 2, 3 -> \x {
182190
# do nothing
@@ -187,13 +195,17 @@ Perl 6
187195
Lambdas in Python can be written as blocks or pointy blocks in Perl 6.
188196
189197
Python
198+
190199
l = lambda i: i + 12
200+
191201
Perl 6
202+
192203
$l = -> $i { $i + 12 }
193204
194205
Another Perl 6 idiom for constructing lambdas is the Whatever star, C<*>.
195206
196207
Perl 6
208+
197209
$l = * + 12 # same as above
198210
199211
A C<*> in an expression will become a placeholder for the argument,
@@ -205,6 +217,7 @@ See the section below for more constructs regarding subroutines and blocks.
205217
Another example (from the Python L<FAQ|https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result>):
206218
207219
Python
220+
208221
squares = []
209222
for x in range(5):
210223
squares.append(lambda: x ** 2)
@@ -213,16 +226,17 @@ Python
213226
# both 16 since there is only one x
214227
215228
Perl 6
216-
my \squares = [];
229+
230+
my @squares;
217231
for ^5 -> \x {
218-
squares.append({ x² });
232+
@squares.append({ x² });
219233
}
220-
say squares[2]();
221-
say squares[4]();
234+
say @squares[2]();
235+
say @squares[4]();
222236
# 4, 16 since each loop iteration has a lexically scoped x,
223237
224238
Note that C<^N> is like C<range(N)>. Similarly,
225-
C<N..^M> works like C<range(N,M)> (a list from N
239+
C<N..^M> works like C<range(N, M)> (a list from N
226240
to M - 1). C<N..M> is a list from N to M. The
227241
C<^> before or after the C<..> indicates that the
228242
beginning or ending endpoint of the list (or both)
@@ -238,6 +252,7 @@ or symbol that can be used in Perl 6 has an ASCII equivalent
238252
=head2 Control Flow
239253
240254
Python has C<for> loops and C<while> loops:
255+
241256
for i in 1, 2:
242257
print i
243258
j = 1
@@ -248,6 +263,7 @@ Python has C<for> loops and C<while> loops:
248263
# 1, 2, 1, 2
249264
250265
Perl 6 also has C<for> loops and C<while> loops:
266+
251267
for 1, 2 -> $i {
252268
say $i
253269
}
@@ -265,6 +281,7 @@ C<break> in Python. C<continue> in Python is C<next>
265281
in Perl 6.
266282
267283
Python
284+
268285
for i in range(10):
269286
if i == 3:
270287
continue
@@ -273,6 +290,7 @@ Python
273290
print i
274291
275292
Perl 6
293+
276294
for ^10 -> $i {
277295
next if $i == 3;
278296
last if $i == 5;
@@ -341,6 +359,7 @@ In Perl 6, positional and named arguments are determined
341359
by the signature of the routine.
342360
343361
Python
362+
344363
def speak(word, times):
345364
for i in range(times):
346365
print word
@@ -399,7 +418,7 @@ Perl 6
399418
$square = { $^x ** 2 }; # placeholder variable
400419
$square = { $_ ** 2 }; # topic variable
401420
402-
Placeholder variables are lexicographically ordered to form positional parameters.
421+
Placeholder variables are put in lexicographic order to form positional parameters.
403422
i.e. these are the same:
404423
405424
$power = { $^x ** $^y }
@@ -418,25 +437,26 @@ Perl 6
418437
( -> \i { i * 2 } for 3, 9 );
419438
( { $^i * 2 } for 3, 9 );
420439
( { $_ * 2 } for 3, 9 );
440+
( * * 2 for 3, 9 );
421441
422442
Conditionals can be applied, but the C<if> comes first,
423443
unlike in Python where the if comes second.
424444
425-
[ x * 2 for x in 1, 2, 3 if x > 1 ]
445+
[ x * 2 for x in 1, 2, 3 if x > 1 ] # Python
426446
427447
vs
428448
429-
( $_ * 2 if $_ > 1 for 1, 2, 3 );
449+
( $_ * 2 if $_ > 1 for 1, 2, 3 ) # Perl 6
430450
431451
For nested loops, the cross product operator C<X>
432452
will help:
433453
434-
[ i + j for i in 3,9 for j in 2,10 ]
454+
[ i + j for i in 3, 9 for j in 2, 10 ] # Python
435455
436456
becomes either of these:
437457
438-
( { $_[0] + $_[1] } for (3,9) X (2,10) );
439-
( -> (\i, \j) { i + j } for (3,9) X (2,10) );
458+
( { $_[0] + $_[1] } for (3, 9) X (2, 10) ); # Perl 6, topic variable
459+
( -> (\i, \j) { i + j } for (3, 9) X (2, 10) ); # Perl 6, pointy block
440460
441461
Using C<map> (which is just like Python's C<map>) is
442462
an alternative.
@@ -447,11 +467,13 @@ Here's an example from the Python L<docs|https://docs.python.org/3/tutorial/clas
447467
First, "instance variables", aka attributes in Perl 6:
448468
449469
Python:
470+
450471
class Dog:
451472
def __init__(self, name):
452473
self.name = name
453474
454475
Perl 6:
476+
455477
class Dog {
456478
has $.name;
457479
}
@@ -460,12 +482,14 @@ Constructors by default take named arguments in Perl 6,
460482
and use the method C<new>.
461483
462484
Python
485+
463486
d = Dog('Fido')
464487
e = Dog('Buddy')
465488
print d.name
466489
print e.name
467490
468491
Perl 6
492+
469493
my $d = Dog.new(:name<Fido>);
470494
my $e = Dog.new(:name<Buddy>);
471495
say $d.name;
@@ -475,6 +499,7 @@ Class attributes in Perl 6 can be declared in a few ways. One way
475499
is to just declare a lexical variable and a method for accessing it.
476500
477501
Python:
502+
478503
class Dog:
479504
kind = 'canine' # class attribute
480505
def __init__(self, name):
@@ -487,6 +512,7 @@ Python:
487512
print e.name
488513
489514
Perl 6:
515+
490516
class Dog {
491517
my $kind = 'canine'; # class attribute
492518
method kind { $kind }
@@ -503,13 +529,15 @@ Perl 6:
503529
To mutate attributes, in Perl 6 you"ll want to use C<is rw>:
504530
505531
Python:
532+
506533
class Dog:
507534
def __init__(self, name):
508535
self.name = name
509536
d = Dog()
510537
d.name = 'rover'
511538
512539
Perl 6:
540+
513541
class Dog {
514542
has $.name is rw;
515543
}
@@ -519,6 +547,7 @@ Perl 6:
519547
Inheritance is done using C<is>:
520548
521549
Python
550+
522551
class Animal:
523552
def jump:
524553
print "I am jumping"
@@ -530,6 +559,7 @@ Python
530559
d.jump()
531560
532561
Perl 6
562+
533563
class Animal {
534564
method jump {
535565
say "I am jumping"
@@ -542,9 +572,10 @@ Perl 6
542572
my $d = Dog.new;
543573
$d.jump;
544574
545-
Multiple inheritance is possible with multiple C<is> traits, or with C<also>.
575+
Multiple inheritance is possible by using C<is> multiple times, or with C<also>.
546576
547577
Python
578+
548579
class Dog(Animal, Friend, Pet):
549580
pass
550581
@@ -563,9 +594,10 @@ or
563594
=head2 Decorators
564595
565596
Decorators in Perl 6 are a way of wrapping a function
566-
in another one. In Perl 6 this is done with C<wrap>.
597+
in another one. In Perl 6, this is done with C<wrap>.
567598
568599
Python
600+
569601
def greeter(f):
570602
def new():
571603
print 'hello'
@@ -579,6 +611,7 @@ Python
579611
world();
580612
581613
Perl 6
614+
582615
sub world {
583616
say 'world'
584617
}
@@ -590,7 +623,7 @@ Perl 6
590623
591624
world;
592625
593-
An alternative would be to use a trait:
626+
To encapsulate the wrapping, it can be done in a trait:
594627
595628
# declare the trait 'greeter'
596629
multi sub trait_mod:<is>(Routine $r, :$greeter) {
@@ -615,7 +648,7 @@ Here's a python context manager that prints the strings
615648
'hello', 'world', and 'bye':
616649
617650
class hello:
618-
def __exit__(self, type, value, traceback):
651+
def __exit__(self, type, value, trace):
619652
print 'bye'
620653
def __enter__(self):
621654
print 'hello'

0 commit comments

Comments
 (0)