Pecha Kucha: Chaos Communication Congress

Download Report

Transcript Pecha Kucha: Chaos Communication Congress

$@%!»; – Perl 6
Frank Blendinger
[email protected]
2013-10-21
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
Disclaimer
07.07.2015 / Method Park Software AG
2
6 != 5
»The Perl 6 design process is about keeping what
works in Perl 5, fixing what doesn't, and adding what's
missing. That means there will be a few fundamental
changes to the language, a large number of extensions
to existing features, and a handful of completely new
ideas. These modifications, enhancements, and
innovations will work together to make the future Perl
even more insanely great – without, we hope, making
it even more greatly insane.«
Damian Conway
07.07.2015 / Method Park Software AG
3
Perl Philosophy
 TIMTOWTDO: There Is More Than One Way To Do It
 DWIM: Do What I Mean
 Keep easy things easy and hard things possible
slurp("filename").lines
 Huffman coding
 Common things → short name
 Crazy stuff → long names
 “Weird things should look weird”
07.07.2015 / Method Park Software AG
4
A Little Perl History

1987: Perl 1.0, 1994: Perl 5.0, 2013-05-18: Perl 5.18

2000: Perl 6 project started

New language – no compatibility with Perl 5
 use v6; *.pl/.pm vs. *.p6
 Intended for friendly coexistence with Perl 5 (still developed)
 Some Perl 6 features backported to Perl 5
(say, given/when, ~~, various CPAN modules: Perl6::*)
07.07.2015 / Method Park Software AG
5
Perl 6: Community Driven Development
»Perl 5 was my rewrite
of Perl. I want Perl 6 to
be the community's
rewrite of Perl and of
the community.«
Larry Wall
07.07.2015 / Method Park Software AG
6
Perl 6 Language Specification
http://perlcabal.org/syn/
07.07.2015 / Method Park Software AG
7
Perl 6 Language Specification
https://github.com/perl6/specs
07.07.2015 / Method Park Software AG
8
Perl 6 Language Specification
07.07.2015 / Method Park Software AG
9
Perl 6 Implementations
»Perl 6 is anything that passes
the official test suite.«
Larry Wall, “Synopsis 1: Overview”
07.07.2015 / Method Park Software AG
10
Perl 6 Compilers / Interpreters / VMs
 Rakudo
 Parrot VM backend
 JVM backend
 Niecza
 compiles to CLR (.NET/Mono)
 Pugs
 one of the first Perl 6 compilers
 no longer maintained
 STD
 Larry Wall’s reference implementation
07.07.2015 / Method Park Software AG
11
Release Date: Christmas
 Rakudo and Niecza are pretty
close
http://perl6.org/compilers/features
 Want to play with Perl 6 right
now? Use Rakudo *!
 Rakudo * = Rakudo Perl 6
compiler, debugger, modules
+ Parrot VM
 Windows installer, Debian
package, cygwin, tarball
 PATH=$PATH:…/rakudo/bin
perl6 helloworld.p6
07.07.2015 / Method Park Software AG
12
Remove Inconsistencies
»In Perl 6, we decided it would
be better to fix the language
than fix the user.«
Larry Wall
07.07.2015 / Method Park Software AG
13
Perl 5
#!/usr/bin/env perl
use strict;
use warnings;
…
07.07.2015 / Method Park Software AG
14
Perl 6
#!/usr/bin/env perl6
use v6;
#
#
#
#
#
implicit strict & warnings pragmas
Can be turned off with:
no strict;
no warnings;
but you don’t want to!
07.07.2015 / Method Park Software AG
15
Sigil Invariance
Perl 5
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
07.07.2015 / Method Park Software AG
16
Sigil Invariance
Perl 5
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar
. "\n";
print @array[0]
. "\n";
print %hash{'foo'} . "\n";
07.07.2015 / Method Park Software AG
# works, but warning
# syntax error
17
Sigil Invariance
Perl 5
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar
. "\n";
print $array[1]
. "\n";
print $hash{'bar'} . "\n";
07.07.2015 / Method Park Software AG
18
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar
~ "\n";
print @array[0]
~ "\n";
print %hash{'foo'} ~ "\n";
07.07.2015 / Method Park Software AG
19
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar
~ "\n";
print @array[0]
~ "\n";
print %hash{'foo'} ~ "\n";
print $array[1]
~ "\n";
# Variable '$array' is not declared.
# Did you mean '@array'?
07.07.2015 / Method Park Software AG
20
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = (1, 2, 3);
my %hash = (foo => 23, bar => 42);
print $scalar
~ "\n";
print @array[0]
~ "\n";
print %hash{'foo'} ~ "\n";
07.07.2015 / Method Park Software AG
21
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = 1, 2, 3;
my %hash = foo => 23, bar => 42;
say $scalar;
say @array[0];
say %hash{'foo'};
07.07.2015 / Method Park Software AG
22
Sigil Invariance
Perl 6
my $scalar = "sclr";
my @array = 1, 2, 3;
my %hash = foo => 23, bar => 42;
say $scalar;
say @array[0];
say %hash<foo>;
07.07.2015 / Method Park Software AG
23
Sigil Invariance
# Perl 5: get list from hash of hashes
my @verbs = @{ $dict { 'verb' }{ 'transitive' } };
# Perl 6: get list from hash of hashes
my @verbs = %dict{ 'verb' }{ 'transitive' };
# Perl 6: or even like this
my @verbs = %dict<verb><transitive>;
07.07.2015 / Method Park Software AG
24
Perl 6 Basics
07.07.2015 / Method Park Software AG
25
Subroutines
sub square-and-sum($x, $y) {
return $x*$x + $y*$y;
}
my $a = 3;
my $b = 4;
say "{$a}² + {$b}² = {square-and-sum($a, $b)}";
# 3² + 4² = 25
07.07.2015 / Method Park Software AG
26
Subroutines
sub stutter($word) {
my $first-letter = substr($word, 0, 1);
my $wwwword = ($first-letter ~ "-") x 3 ~ $word;
say $wwwword;
}
stutter("what");
07.07.2015 / Method Park Software AG
# w-w-w-what
27
Subroutines
sub stutterify($word) {
my $first-letter = substr($word, 0, 1);
my $wwwword = ($first-letter ~ "-") x 3 ~ $word;
$word = $wwwword;
# no-no! $word is read-only!
}
07.07.2015 / Method Park Software AG
28
Subroutines
sub stutterify($word is rw) {
my $first-letter = substr($word, 0, 1);
my $wwwword = ($first-letter ~ "-") x 3 ~ $word;
$word = $wwwword;
}
07.07.2015 / Method Park Software AG
29
Subroutines
sub stutterify($word is rw) {
my $first-letter = substr($word, 0, 1);
my $wwwword = ($first-letter ~ "-") x 3 ~ $word;
$word = $wwwword;
}
my $word = "hello";
$word.say;
stutterify($word);
$word.say;
07.07.2015 / Method Park Software AG
# hello
# h-h-h-hello
30
Subroutines
# pass-by-value
sub say-next-num($num is copy) {
say ++$num;
}
my $num-of-truth = 42;
say-next-num($num-of-truth);
say "The truth is still $num-of-truth";
07.07.2015 / Method Park Software AG
31
Dealing with Arrays
sub shout-words(@words) {
for @words -> $word {
say uc($word);
}
}
07.07.2015 / Method Park Software AG
32
Dealing with Arrays
sub shout-words(@words) {
for @words {
say uc($_);
# topic variable
}
}
07.07.2015 / Method Park Software AG
33
Dealing with Arrays
sub shout-words(@words) {
for @words { say uc($_) }
}
my @words = <hey ho let's go>;
shout-words(@words);
#
#
#
#
HEY
HO
LET'S
GO
07.07.2015 / Method Park Software AG
34
Dealing with Hashes
sub say-names-with-age(%people) {
for %people.kv -> $key, $value {
say $key ~ " (" ~ $value ~ ")";
}
}
07.07.2015 / Method Park Software AG
35
Dealing with Hashes
sub say-names-with-age(%people) {
for %people {
say $_.key ~ " (" ~ $_.value ~ ")";
}
}
07.07.2015 / Method Park Software AG
36
Dealing with Hashes
sub say-names-with-age(%people) {
for %people {
say .key ~ " (" ~ .value ~ ")";
}
}
07.07.2015 / Method Park Software AG
37
Dealing with Hashes
sub say-names-with-age(%people) {
for %people {
say .key ~ " (" ~ .value ~ ")";
}
}
my %white-family = Walter
=> 52, Skyler => 41,
"Walter Jr." => 17, Holly => 2;
say-names-with-age(%white-family);
07.07.2015 / Method Park Software AG
38
Optional Parameters
sub whatever($optional?) {
if defined($optional) {
say "$optional is pretty cool.";
} else {
say "Whatever, man.";
}
}
whatever("The dude");
whatever();
07.07.2015 / Method Park Software AG
39
Named Parameters
sub protip(:$what, :$who) {
say "Use $what, $who!";
}
protip(:what("the force"), :who("Luke"));
protip(:who("Luke"), :what("the force"));
07.07.2015 / Method Park Software AG
40
Named Parameters
sub protip(:$what, :$who) {
say "Use $what, $who!";
}
# short form for strings
protip(:who<Luke>, :what<the force>);
07.07.2015 / Method Park Software AG
41
Named Parameters
sub protip(:$what, :$who) {
say "Use $what, $who!";
}
my $thing = "the force";
my $guy
= "Luke";
#protip( $thing, $guy);
# Nope.
#protip(:$thing, :$guy);
# Also, nope.
protip(:what($thing), :who($guy));
07.07.2015 / Method Park Software AG
42
Named Parameters
sub protip(:$what, :$who) {
say "Use $what, $who!";
}
my $what = "the force";
my $who = "Luke";
protip(:$who, :$what); # names match → magic!
07.07.2015 / Method Park Software AG
43
Required Named Parameters
sub protip(:$what, :$who) {
say "Use $what, $who!";
}
# named params are optional by default
protip();
# use of uninitialized value of type …
07.07.2015 / Method Park Software AG
44
Required Named Parameters
sub protip(:$what!, :$who!) {
say "Use $what, $who!";
}
# named params are optional by default
protip();
# Required named parameter 'what' not passed
07.07.2015 / Method Park Software AG
45
Varargs with Slurpy Arrays
# varargs with slurpy arrays (* prefix)
sub sum(*@nums) {
[+] @nums;
}
say sum 42, 23, 1337;
07.07.2015 / Method Park Software AG
46
Varargs with Slurpy Arrays
# default (no "()" given!) is *@_, so:
sub sum {
[+] @_;
}
say sum 42, 23, 1337;
07.07.2015 / Method Park Software AG
47
Procedural vs. Object Oriented
 Perl 6 is object-oriented at its core
 You can use it, but you don’t have to (TIMTOWTDI!)
07.07.2015 / Method Park Software AG
48
Procedural vs. Object Oriented
 Perl 6 is object-oriented at its core
 You can use it, but you don’t have to (TIMTOWTDI!)
# procedural
say split(" ", capitalize("hello world"))[1];
# World
07.07.2015 / Method Park Software AG
49
Procedural vs. Object Oriented
 Perl 6 is object-oriented at its core
 You can use it, but you don’t have to (TIMTOWTDI!)
# procedural
say split(" ", capitalize("hello world"))[1];
# World
# object oriented
"hello world".capitalize.split(" ")[1].say;
# World
07.07.2015 / Method Park Software AG
50
Objects
Everything is an object
my $x = "foo";
my $y = 42;
my $z = Int;
07.07.2015 / Method Park Software AG
# Str object
# Int object
# Int class
51
Objects
Everything is an object
my $x = "foo";
my $y = 42;
my $z = Int;
# Str object
# Int object
# Int class
my &code = { say "Ohai." };
&code();
07.07.2015 / Method Park Software AG
52
Objects
Everything is an object
my $x = "foo";
my $y = 42;
my $z = Int;
# Str object
# Int object
# Int class
my &code = { say "Ohai." };
&code();
&code = sub ($name) { say "Ohai {$name}." };
&code("Alice");
07.07.2015 / Method Park Software AG
53
Introspection
What are you?
42.WHAT;
(1/2).WHAT;
07.07.2015 / Method Park Software AG
# (Int)
# (Rat)
54
Introspection
What are you?
42.WHAT;
(1/2).WHAT;
# (Int)
# (Rat)
What can you do for me?
42.^methods();
07.07.2015 / Method Park Software AG
#
#
#
#
Int Num Rat FatRat abs Bridge chr
sqrt base expmod is-prime floor
ceiling round lsb msb sign conj rand
sin …
55
Type Objects vs. Instance Objects
my $i = Int;
say $i.WHAT;
say $i.defined;
say $i;
# (Int)
# False
# (Int)
$i = Int.new;
say $i.WHAT;
say $i.defined;
say $i;
#
#
#
#
07.07.2015 / Method Park Software AG
or: $i = $i.new
(Int)
True
0
or:
$i .= new;
56
Perlify!
my %complicatedObj = Hash.new;
%complicatedObj{'string'}
%complicatedObj{'list'}
%complicatedObj{'parcel'}
%complicatedObj{'range'}
%complicatedObj{'inner hash'}
=
=
=
=
=
"this is foo";
[4, 5, 6];
4, 5, 6;
10..15;
x => 1.1, y => 2.5;
%complicatedObj.perl.say;
# ("string" => "this is foo", "list" => [4, 5, 6],
"parcel" => $(4, 5, 6), "range" => 10..15,
"inner hash" => $("x" => 1.1, "y" => 2.5)).hash
07.07.2015 / Method Park Software AG
57
We Have to Go Deeper!
07.07.2015 / Method Park Software AG
58
DUMP
my %complicatedObj = Hash.new;
%complicatedObj{'string'}
%complicatedObj{'list'}
%complicatedObj{'parcel'}
%complicatedObj{'range'}
%complicatedObj{'inner hash'}
=
=
=
=
=
"this is foo";
[4, 5, 6];
4, 5, 6;
10..15;
x => 1.1, y => 2.5;
%complicatedObj.DUMP.say;
07.07.2015 / Method Park Software AG
59
DUMP
Hash<1>(
:$!descriptor(
Perl6::Metamodel::ContainerDescriptor<2>(...)),
:$!storage(Hash<3>(
string => ▶"this is foo",
list => ▶Array<5>(
range => ▶Range<14>(
:$!flattens(True),
:min(▶10),
:$!items(QRPA<7>(
:max(▶15),
▶4,
:excludes_min(▶False),
▶5,
:excludes_max(▶False)
▶6
),
)),
inner hash => ▶Parcel<18>(:$!storage(RPA<19>(
:$!nextiter(▶Mu)
Pair<20>(
),
:key(▶"x"),
parcel => ▶Parcel<12>(:$!storage(RPA<13>(
:value(▶1.1)
=Int<8>,
),
=Int<9>,
Pair<23>(
=Int<10>
:key(▶"y"),
))),
:value(▶2.5)
)
)))
))
)
07.07.2015 / Method Park Software AG
60
DUMP
Hash<1>(
:$!descriptor(
Perl6::Metamodel::ContainerDescriptor<2>(...)),
:$!storage(Hash<3>(
string => ▶"this is foo",
list => ▶Array<5>(
range => ▶Range<14>(
:$!flattens(True),
:min(▶10),
:$!items(QRPA<7>(
:max(▶15),
▶4,
:excludes_min(▶False),
▶5,
:excludes_max(▶False)
▶6
),
)),
inner hash => ▶Parcel<18>(:$!storage(RPA<19>(
:$!nextiter(▶Mu)
Pair<20>(
),
:key(▶"x"),
parcel => ▶Parcel<12>(:$!storage(RPA<13>(
:value(▶1.1)
=Int<8>,
),
=Int<9>,
Pair<23>(
=Int<10>
:key(▶"y"),
))),
:value(▶2.5)
)
)))
))
)
07.07.2015 / Method Park Software AG
61
Type System: Dynamic Types
my @untypedArray;
@untypedArray.push(23).push("foo").push([1, 2]);
@untypedArray.perl.say;
# Array.new(23, "foo", [1, 2])
07.07.2015 / Method Park Software AG
62
Type System: Static Types
my @untypedArray;
@untypedArray.push(23).push("foo").push([1, 2]);
@untypedArray.perl.say;
# Array.new(23, "foo", [1, 2])
my Int @intArray;
@intArray.push(23).push("foo");
# Type check failed in .push; expected 'Int' but got
'Str'
07.07.2015 / Method Park Software AG
63
Beyond Type Safety: Contraints
sub div(Real $x,
Real $y where $y != 0) { $x / $y };
div(1, 2).say;
# 0.5
div(1, 0).say;
# Constraint type check failed for parameter '$y'
07.07.2015 / Method Park Software AG
64
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2,
Int $height where 1..10 = $width) {
for ^$height {
for ^$width {
print "#";
}
print "\n";
}
}
07.07.2015 / Method Park Software AG
65
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2,
Int $height where 1..10 = $width) {
for ^$height {
for ^$width {
print "#";
}
print "\n";
}
}
draw-ascii-rectangle(10, 1);
draw-ascii-rectangle();
draw-ascii-rectangle(4);
07.07.2015 / Method Park Software AG
66
Default Parameters
sub draw-ascii-rectangle(Int $width where 1..10 = 2,
Int $height where 1..10 = $width) {
for ^$height {
for ^$width {
print "#";
}
print "\n";
}
##########
}
##
##
draw-ascii-rectangle(10, 1);
####
draw-ascii-rectangle();
####
####
draw-ascii-rectangle(4);
####
07.07.2015 / Method Park Software AG
67
Object Oriented Programming
07.07.2015 / Method Park Software AG
68
Classes
class Point {
has $!x;
has $!y;
# private
# private
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new;
07.07.2015 / Method Park Software AG
69
Classes
class Point {
has $!x;
has $!y;
# private
# private
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new;
$point.to-string().say; # use of unit. value…
#$point.x.say;
$point.DUMP.say;
07.07.2015 / Method Park Software AG
# no access, private
# Point<1>(:$!x(Any), :$!y(Any))
70
Classes
class Point {
has $.x;
has $.y;
# public
# public
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new(:x(1), :y(2));
$point.to-string().say;
07.07.2015 / Method Park Software AG
# [1/2]
71
Classes
class Point {
has $.x;
has $.y;
# public
# public
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new(x => 1, y => 2);
$point.to-string().say;
07.07.2015 / Method Park Software AG
# [1/2]
72
Attributes vs. Accessor Methods
class Foo {
has $.bar;
method bar { $!bar * 10 }
method use-attribute { say "bar is $!bar" }
method use-accessor { say "bar is $.bar" }
}
Foo.new(bar => 10).use-attribute;
Foo.new(bar => 10).use-accessor;
07.07.2015 / Method Park Software AG
# bar is 10
# bar is 100
73
Read/Write Attributes
class Point {
has $.x;
has $.y;
# public
# public
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new(x => 1, y => 2);
$point.x = 23;
07.07.2015 / Method Park Software AG
# NOPE! $!x is readonly
74
Read/Write Attributes
class Point {
has $.x is rw;
has $.y is rw;
method to-string() { "[$!x/$!y]"; }
}
my $point = Point.new(x => 1, y => 2);
$point.x = 23;
$point.y = 42;
$point.to-string().say;
07.07.2015 / Method Park Software AG
# [23/42]
75
Default Values for Attributes
class Point {
has $.x = 0;
has $.y = $!x;
method to-string() { "[$!x/$!y]"; }
}
Point.new.to-string.say;
# [0/0]
Point.new(x => 9).to-string.say; # [9/9]
07.07.2015 / Method Park Software AG
76
Inheritance
class Mother { method i-am { say "I am your mother" } }
class Father { method i-am { say "I am your father" } }
class Child is Father is Mother {}
Child.new.i-am;
07.07.2015 / Method Park Software AG
77
Inheritance
class Mother { method i-am { say "I am your mother" } }
class Father { method i-am { say "I am your father" } }
class Child is Father is Mother {}
Child.new.i-am;
Child.^mro.say;
07.07.2015 / Method Park Software AG
# I am your father
# (Child) (Father) (Mother) (Any) (Mu)
78
Inheritance – Private vs. Public Methods
class Dog {
method !do-private { say "This is dog." }
method do-public
{ self!do-private }
}
class Pug is Dog {
method access-dog-public { self.do-public }
method access-dog-private { self!do-private }
}
Dog.new.do-public;
07.07.2015 / Method Park Software AG
79
Inheritance – Private vs. Public Methods
class Dog {
method !do-private { say "This is dog." }
method do-public
{ self!do-private }
}
class Pug is Dog {
method access-dog-public { self.do-public }
method access-dog-private { self!do-private }
}
Dog.new.do-public;
Pug.new.do-public;
07.07.2015 / Method Park Software AG
# ok
80
Inheritance – Private vs. Public Methods
class Dog {
method !do-private { say "This is dog." }
method do-public
{ self!do-private }
}
class Pug is Dog {
method access-dog-public { self.do-public }
method access-dog-private { self!do-private }
}
Dog.new.do-public;
Pug.new.do-public;
# ok
# ok
Pug.new.access-dog-public;
07.07.2015 / Method Park Software AG
81
Inheritance – Private vs. Public Methods
class Dog {
method !do-private { say "This is dog." }
method do-public
{ self!do-private }
}
class Pug is Dog {
method access-dog-public { self.do-public }
method access-dog-private { self!do-private }
}
Dog.new.do-public;
Pug.new.do-public;
# ok
# ok
Pug.new.access-dog-public;
Pug.new.access-dog-private;
# ok
07.07.2015 / Method Park Software AG
82
Inheritance – Private vs. Public Methods
class Dog {
method !do-private { say "This is dog." }
method do-public
{ self!do-private }
}
class Pug is Dog {
method access-dog-public { self.do-public }
method access-dog-private { self!do-private }
}
Dog.new.do-public;
Pug.new.do-public;
# ok
# ok
Pug.new.access-dog-public;
Pug.new.access-dog-private;
# ok
# NOPE
07.07.2015 / Method Park Software AG
83
Roles
role Happy { method have-fun { say ":-)" } }
role Sad
{ method frown
{ say ":-/" } }
class Guy does Happy does Sad {
method code-some-perl6
{ self.have-fun }
method do-powerpoint-pres { self.frown }
}
07.07.2015 / Method Park Software AG
84
Roles
role Happy { method have-fun { say ":-)" } }
role Sad
{ method frown
{ say ":-/" } }
class Guy does Happy does Sad {
method code-some-perl6
{ self.have-fun }
method do-powerpoint-pres { self.frown }
}
my $me = Guy.new;
$me.code-some-perl6;
$me.do-powerpoint-pres;
$me.have-fun;
07.07.2015 / Method Park Software AG
# :-)
# :-/
# :-)
85
Stubs
role AbstractSerializable { method serialize() { ... } }
07.07.2015 / Method Park Software AG
86
Stubs
role AbstractSerializable { method serialize() { ... } }
# not allowed:
class APoint does AbstractSerializable {
has $.x;
has $.y;
} # -> Method 'serialize' must be implemented by APoint…
07.07.2015 / Method Park Software AG
87
Stubs
role AbstractSerializable { method serialize() { ... } }
# not allowed:
class APoint does AbstractSerializable {
has $.x;
has $.y;
} # -> Method 'serialize' must be implemented by APoint…
# this works:
class SPoint does AbstractSerializable {
has $.x;
has $.y;
method serialize() { "p($.x, $.y)" }
}
07.07.2015 / Method Park Software AG
88
Functional Programming
07.07.2015 / Method Park Software AG
89
Closures
my @times = ();
# Array.new
for 1..10 {
my $multiplicant = $_;
@times[$_] = sub ($a) { $a * $multiplicant };
}
07.07.2015 / Method Park Software AG
90
Closures
my @times = ();
# Array.new
for 1..10 {
my $multiplicant = $_;
@times[$_] = sub ($a) { $a * $multiplicant };
}
say @times[3](4);
say @times[5](20);
say @times[7](3);
07.07.2015 / Method Park Software AG
# 12
# 100
# 21
91
Infinite Lists, Lazy Evaluation
my @all-the-integers = 0..Inf;
for ^10 { say @all-the-integers[$_] }
07.07.2015 / Method Park Software AG
92
Infinite Lists, Lazy Evaluation
my @all-the-integers = 0..Inf;
for ^10 { say @all-the-integers[$_] }
07.07.2015 / Method Park Software AG
93
map
07.07.2015 / Method Park Software AG
94
map
(1..10).map( { $_ * $_ } ).join(", ").say;
07.07.2015 / Method Park Software AG
95
map
(1..10).map( { $_ * $_ } ).join(", ").say;
# 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
07.07.2015 / Method Park Software AG
96
Z – the zip operator
07.07.2015 / Method Park Software AG
97
Z – the zip operator
my @zipped-array = zip (1, 2, 3), (10, 20, 30);
@zipped-array.join(", ").say;
# 1, 10, 2, 20, 3, 30
07.07.2015 / Method Park Software AG
98
Z – the zip operator
my @zipped-array = (1, 2, 3) Z (10, 20, 30);
@zipped-array.join(", ").say;
# 1, 10, 2, 20, 3, 30
07.07.2015 / Method Park Software AG
99
Z – the zip operator
Application: iterating two lists in parallel
sub are-pairwise-greater(@array1, @array2) {
for @array1 Z @array2 -> $x, $y {
return False if $x <= $y;
}
return True;
}
07.07.2015 / Method Park Software AG
100
Z – the zip operator
Application: iterating two lists in parallel
sub are-pairwise-greater(@array1, @array2) {
for @array1 Z @array2 -> $x, $y {
return False if $x <= $y;
}
return True;
}
are-pairwise-greater(2..11, 1..10).say;
#
are-pairwise-greater(2..11, 1..5).say;
#
are-pairwise-greater(2..3, 1..15).say;
#
are-pairwise-greater((3,3,3), (1,2,3)).say; #
07.07.2015 / Method Park Software AG
True
True
True
False
101
Meta Operators
07.07.2015 / Method Park Software AG
102
Meta Operators
Reduce meta operator: [op]
Infix operator → list operator
1 + $x + 5 + $y
[+] (1, $x, 5, $y)
07.07.2015 / Method Park Software AG
103
Meta Operators
# multiply all elements of @a
my $prod = [*] @a;
# calculate mean of @a
my $mean = ([+] @a) / @a.elems;
# true if elements of @a are numerically sorted
my $sorted = [<=] @a;
# find the smallest element of @a and @b combined
my $min = [min] @a, @b;
07.07.2015 / Method Park Software AG
104
The Hyper Meta Operators: » / «
07.07.2015 / Method Park Software AG
105
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10;
my @b = 11..20;
my @sums = ();
for @a Z @b -> $x, $y {
@sums.push($x + $y);
}
@sums.join(", ").say;
07.07.2015 / Method Park Software AG
106
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10;
my @b = 11..20;
(@a »+« @b).join(", ").say;
07.07.2015 / Method Park Software AG
107
The Hyper Operators: » / «
Sum of two lists
my @a = 1..10;
my @b = 11..20;
(@a »+« @b).join(", ").say;
# >>+<< works too.
# But it’s not that cool.
07.07.2015 / Method Park Software AG
108
The Hyper Operators: » / «
# increment all elements
@a = 1, 7, 9;
@a»++;
@a.join(", ").say;
# get pairwise minimum
@a = 1, 7, 9;
@b = 4, 2, 9;
my @minima = @a »min« @b;
@minima.join(", ").say;
07.07.2015 / Method Park Software AG
109
Multi
07.07.2015 / Method Park Software AG
110
Multi
class Human
{ has $.name }
class CodeMonkey is Human {}
class BusinessGuy is Human {}
multi sub greet(Human
$h) { say "Hello {$h.name}." }
multi sub greet(CodeMonkey $h) { say "Hey {$h.name}, sup dude?" }
multi sub greet(BusinessGuy $h) { say "Nice to meet you, {$h.name}."}
07.07.2015 / Method Park Software AG
111
Multi
class Human
{ has $.name }
class CodeMonkey is Human {}
class BusinessGuy is Human {}
multi sub greet(Human
$h) { say "Hello {$h.name}." }
multi sub greet(CodeMonkey $h) { say "Hey {$h.name}, sup dude?" }
multi sub greet(BusinessGuy $h) { say "Nice to meet you, {$h.name}."}
my $john = CodeMonkey.new(name => "John Johnson");
my $jack = BusinessGuy.new(name => "Jack Jackson");
greet $john;
# Hey John Johnson, sup dude?
greet $jack;
# Nice to meet you, Jack Jackson.
07.07.2015 / Method Park Software AG
112
Multi with Constraints
multi sub mysign(Int $x where $x == 0) { 0 }
multi sub mysign(Int $x where $x < 0) { -1 }
multi sub mysign(Int $x)
{ +1 }
for -2, 0, 2 { say mysign($_) }
# -1
# 0
# 1
07.07.2015 / Method Park Software AG
113
Command Line Parsing with multi MAIN
multi
multi
multi
multi
sub
sub
sub
sub
MAIN('add‚ ,
MAIN('sub',
MAIN('mult',
MAIN('div',
$x,
$x,
$x,
$x,
$y)
$y)
$y)
$y)
{
{
{
{
say
say
say
say
"$x
"$x
"$x
"$x
+
*
/
$y
$y
$y
$y
=
=
=
=
{$x
{$x
{$x
{$x
+
*
/
$y}"
$y}"
$y}"
$y}"
}
}
}
}
│% ./cmdline_args.p6
│Usage:
│ ./cmdline_args.p6 add <x> <y>
│ ./cmdline_args.p6 sub <x> <y>
│ ./cmdline_args.p6 mult <x> <y>
│ ./cmdline_args.p6 div <x> <y>
07.07.2015 / Method Park Software AG
114
Command Line Parsing with multi MAIN
multi
multi
multi
multi
sub
sub
sub
sub
MAIN('add‚ ,
MAIN('sub',
MAIN('mult',
MAIN('div',
$x,
$x,
$x,
$x,
$y)
$y)
$y)
$y)
{
{
{
{
say
say
say
say
"$x
"$x
"$x
"$x
+
*
/
$y
$y
$y
$y
=
=
=
=
{$x
{$x
{$x
{$x
+
*
/
$y}"
$y}"
$y}"
$y}"
}
}
}
}
│% ./cmdline_args.p6 add 23 42
│23 + 42 = 65
│% ./cmdline_args.p6 mult 23 42
│23 * 42 = 966
07.07.2015 / Method Park Software AG
115
given/when
07.07.2015 / Method Park Software AG
116
given/when
my $thing = 17.23;
given $thing {
when
Bool
when
Real
default
}
07.07.2015 / Method Park Software AG
{ say "yes/no"
}
{ say "number"
}
{ say $thing.WHAT }
117
given/when
my $n = 17;
given $n {
when
$n < 10 { say "small" }
when
$n < 100 { say "medium" }
default
{ say "big"
}
}
07.07.2015 / Method Park Software AG
118
given/when
my ($x, $y) = 12, 23;
given $x, $y {
when $x < $y
when $x > $y
when $x == $y
}
07.07.2015 / Method Park Software AG
{ say "smaller"
{ say "greater"
{ say "same" }
}
}
119
Smart Matching with ~~
07.07.2015 / Method Park Software AG
120
Smart Matching with ~~
class A {}
class B is A {}
class C {}
my $b = B.new;
say "b is a A" if $b ~~ A;
say "b is a B" if $b ~~ B;
say "b is a C" if $b ~~ C;
07.07.2015 / Method Park Software AG
# yep.
# yep.
# certainly not
121
Smart Matching with ~~
my $num = 23;
say "num has two digits" if $num ~~ 10..99;
my @list1 = 1, 2, 3, 4;
my @list2 = 1..4;
say "list are identical" if @list1 ~~ @list2;
07.07.2015 / Method Park Software AG
122
Smart Matching with ~~
my @list = 1, 2, 3;
say (1 ~~ any(@list)) ?? "yes" !! "nope" ;
say (6 ~~ any(@list)) ?? "yes" !! "nope" ;
my $text = "sOme tExT hEre";
say "matches" if $text ~~ m:i/text/;
07.07.2015 / Method Park Software AG
123
One More Thing

Awesome new regex syntax

… and beyond: rules, tokens, grammars

It’s a parsing wonderland!
07.07.2015 / Method Park Software AG
124
One More Thing

Awesome new regex syntax

… and beyond: rules, tokens, grammars

It’s a parsing wonderland!
Maybe another talk,
if anyone cares. :-)
07.07.2015 / Method Park Software AG
125
References
 http://perl6.org/
#perl6 @ FreeNode
 https://github.com/perl6
 https://github.com/perl6/book/
 http://perl6advent.wordpress.com/
 http://perl6maven.com/
 http://rakudo.org/
07.07.2015 / Method Park Software AG
126
Questions?
07.07.2015 / Method Park Software AG
127