joy of six - discover the joy of perl 6

45
THE JOY OF SIX Nigel Hamilton + Team Lean Software Development + Training Bath, United Kingdom http://nigelhamilton.com [email protected]

Upload: trexy

Post on 17-Feb-2017

318 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Joy of Six - Discover the Joy of Perl 6

THE JOY OF SIX

Nigel Hamilton + Team

Lean Software Development + TrainingBath, United Kingdom

http://[email protected]

Page 2: Joy of Six - Discover the Joy of Perl 6

First a confession ...

Page 3: Joy of Six - Discover the Joy of Perl 6

THE JOY OF SEX

Page 4: Joy of Six - Discover the Joy of Perl 6

OMG – It's THIS Christmas!!!

Page 5: Joy of Six - Discover the Joy of Perl 6
Page 6: Joy of Six - Discover the Joy of Perl 6
Page 7: Joy of Six - Discover the Joy of Perl 6
Page 8: Joy of Six - Discover the Joy of Perl 6

O(fun)

Page 9: Joy of Six - Discover the Joy of Perl 6
Page 10: Joy of Six - Discover the Joy of Perl 6
Page 11: Joy of Six - Discover the Joy of Perl 6
Page 12: Joy of Six - Discover the Joy of Perl 6
Page 13: Joy of Six - Discover the Joy of Perl 6
Page 14: Joy of Six - Discover the Joy of Perl 6

Jonathan Worthington

“Perl6: my most learningist project”

Jnthn++

Page 15: Joy of Six - Discover the Joy of Perl 6
Page 16: Joy of Six - Discover the Joy of Perl 6
Page 17: Joy of Six - Discover the Joy of Perl 6

Wetware <=> Hardware

Page 18: Joy of Six - Discover the Joy of Perl 6

O(fun) needs Cognitive Comfort

Page 19: Joy of Six - Discover the Joy of Perl 6
Page 20: Joy of Six - Discover the Joy of Perl 6

PCRE

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

Page 21: Joy of Six - Discover the Joy of Perl 6

Fear is a Flow killer

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

Page 22: Joy of Six - Discover the Joy of Perl 6

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]>/;;

Page 23: Joy of Six - Discover the Joy of Perl 6

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';}

Page 24: Joy of Six - Discover the Joy of Perl 6

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";}

Page 25: Joy of Six - Discover the Joy of Perl 6

Grammar Time!grammar IP::Address { token TOP { <v4> } token v4 { <digits> '.' <digits> '.' <digits> '.' <digits> } token digits { (\d**1..3) <?{ $0 < 256 }> }}

Page 26: Joy of Six - Discover the Joy of Perl 6

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

Page 27: Joy of Six - Discover the Joy of Perl 6

Rosetta Stone Code

A+ to ZED 596 Languages Hundreds of Tasks Where Larry Wall has

O(fun) http://rosettacode.org

Page 28: Joy of Six - Discover the Joy of Perl 6

Whipupitude + Expressivity

Page 29: Joy of Six - Discover the Joy of Perl 6

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;

Page 30: Joy of Six - Discover the Joy of Perl 6

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

Page 31: Joy of Six - Discover the Joy of Perl 6

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

Page 32: Joy of Six - Discover the Joy of Perl 6

Expressivity # Perl5 old Skool sub factorial { my $n = shift; my $result = 1; for (my $i = 1; $i <= $n; ++$i) { $result *= $i; }; return $result; }

Page 33: Joy of Six - Discover the Joy of Perl 6

Iterative / Recursive / Declarative? Expressive

sub postfix:<!> { [*] 1 .. $^n }   say 5!; # prints 120

Page 34: Joy of Six - Discover the Joy of Perl 6

[Meta] WAT? - operators on operators

Page 35: Joy of Six - Discover the Joy of Perl 6

[Reduction Meta Operator] my @numbers = 1 .. 3; say [+] @numbers; say [max] @numbers; say [min] @numbers; say [~] @numbers; say [>] @numbers; say [<] @numbers; say [>] @numbers.reverse;

Page 36: Joy of Six - Discover the Joy of Perl 6

[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

Page 37: Joy of Six - Discover the Joy of Perl 6

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♠

Page 38: Joy of Six - Discover the Joy of Perl 6

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 ...

Page 39: Joy of Six - Discover the Joy of Perl 6

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

Page 40: Joy of Six - Discover the Joy of Perl 6

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

Page 41: Joy of Six - Discover the Joy of Perl 6

Whipupitude + Expressivity

Page 42: Joy of Six - Discover the Joy of Perl 6
Page 43: Joy of Six - Discover the Joy of Perl 6

Discover the Joy of Six

Page 44: Joy of Six - Discover the Joy of Perl 6

The O(fun) must Flow!

Page 45: Joy of Six - Discover the Joy of Perl 6

Go to http://perl6.org and http://perl6intro.com