THE JOY OF SIX
Nigel Hamilton + Team
Lean Software Development + TrainingBath, United Kingdom
http://[email protected]
First a confession ...
THE JOY OF SEX
OMG – It's THIS Christmas!!!
O(fun)
Jonathan Worthington
“Perl6: my most learningist project”
Jnthn++
Wetware <=> Hardware
O(fun) needs Cognitive Comfort
PCRE
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
Fear is a Flow killer
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
Human-friendly Regex Parsingmy $flavour-of-perl = 'Perl 6';
say 'Perl 5 regex matches' if $flavour-of-perl ~~ m:P5/Perl [56]/;
say 'Perl 6 regex matches' if $flavour-of-perl ~~ m:i/perl \s <[56]>/;;
Regex Revamp
my $ip = '213.120.160.146';
if $ip ~~ m:P5/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ { say 'Perl 5 regex matches'; }
if $ip ~~ /^[<digit> ** 1..3] ** 4 % '.'$/ { say 'Perl 6 regex matches';}
Ramping Regexen
my token digits { (<digit>**1..3) <?{ $0 < 256 }> };
my rule ip-address { <digits> '.' <digits> '.' <digits> '.' <digits> };
my $ip = '213.120.160.146';
if ($ip ~~ /<ip-address>/) { say "Perl 6 rule matches";}
Grammar Time!grammar IP::Address { token TOP { <v4> } token v4 { <digits> '.' <digits> '.' <digits> '.' <digits> } token digits { (\d**1..3) <?{ $0 < 256 }> }}
Comb the Logs
use IP::Address;
my $logs = 'Check these version 4 IPs: 213.120.160.146, 12.10.12.13';
my @ips = $logs.comb(/<IP::Address::v4>/);
IPv6 too? http://rosettacode.org/wiki/Parse_an_IP_Address#Perl_6
Rosetta Stone Code
A+ to ZED 596 Languages Hundreds of Tasks Where Larry Wall has
O(fun) http://rosettacode.org
Whipupitude + Expressivity
Whipupitude// Javapublic class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal System.out.println("Hello, World"); }}
# Perl 6say 'Hello, World';'Hello, World'.say;
Whipupitude
#!/usr/bin/env perl6
#| deploy to a node by namesub MAIN ($node-name) { say "deploying now to node $node-name";}
shell> ./deploy.p6 -helpUsage: ./deploy.p6 <node-name> -- deploy to a node by name
Whipupitude
use IP::Address;
#| deploy to a node by namemulti sub MAIN ($node-name) { say "deploying to node: $node-name";}
#| deploy to a specific IP addressmulti sub MAIN ($node-ip where { $_ ~~ /<IP::Address::v4>/ }) { say "deploying to ip address: $node-ip";}shell> ./deploy.p6 -helpUsage: ./deploy.p6 <node-name> -- deploy to a node by name ./deploy.p6 <node-ip> -- deploy to a specific IP address
Expressivity # Perl5 old Skool sub factorial { my $n = shift; my $result = 1; for (my $i = 1; $i <= $n; ++$i) { $result *= $i; }; return $result; }
Iterative / Recursive / Declarative? Expressive
sub postfix:<!> { [*] 1 .. $^n } say 5!; # prints 120
[Meta] WAT? - operators on operators
[Reduction Meta Operator] my @numbers = 1 .. 3; say [+] @numbers; say [max] @numbers; say [min] @numbers; say [~] @numbers; say [>] @numbers; say [<] @numbers; say [>] @numbers.reverse;
[Reduction Meta Operator] my @numbers = 1 .. 3; say [+] @numbers; # 6 say [max] @numbers; # 3 say [min] @numbers; # 1 say [~] @numbers; # 123 say [>] @numbers; # False say [<] @numbers; # True say [>] @numbers.reverse; # True
X product – Meta Operator my @suits = <♣ ♢ ♡ ♠>; my @ranks = flat 1..10,'J','Q','K','A'; my @deck = @ranks X~ @suits; my @hand = @deck.pick(5); say @hand; shell> perl6 hand.p6 7♣ 5♡ 2♣ J♠ 10♠
Lean Coding ... BOSS: “I need dates for all the last Fridays of the month for
this year” Lean programmer grabs a highlighter pen and a calendar. BOSS: “Oh, and for any year out of the last 20” Lean programmer opens up a terminal window … and types ...
Python's Last Friday (14 LOC) import calendar c=calendar.Calendar() fridays={} year=raw_input("year") for item in c.yeardatescalendar(int(year)): for i1 in item: for i2 in i1: for i3 in i2: if "Fri" in i3.ctime() and year in i3.ctime():
month,day=str(i3).rsplit("-",1) fridays[month]=day
Last Friday in Perl 6 (7 LOC) sub MAIN (Int :$year = Date.today.year) { my @fri; for Date.new("$year-01-01") .. Date.new("$year-12-31"){ @fri[.month] = .Str if .day-of-week == 5; } .say for @fri[1..12]; } shell> perl6 last-friday.p6 --help Usage: last-friday.p6 [--year=<Int>] shell> perl6 last-friday.p6 2015-12-25
Whipupitude + Expressivity
Discover the Joy of Six
The O(fun) must Flow!
Go to http://perl6.org and http://perl6intro.com
Top Related