Slide 1

Slide 1 text

Perl 6 functional

Slide 2

Slide 2 text

Previously: general Array / Hash Regex OOP operator

Slide 3

Slide 3 text

func. programming What is it? What is it good for? How much can P6? How it's done in P6?

Slide 4

Slide 4 text

Features: parameter, functions, HOP (abstract) types, -classes, -inferenz lists-, comprehension, generators, tuple, currying,folding,map,filter pattern matching (MMD)

Slide 5

Slide 5 text

Funky Features: We have almost all!

Slide 6

Slide 6 text

Funky Features: We have almost all!

Slide 7

Slide 7 text

Perl Philosophy moe than 1 Way: programming for adults

Slide 8

Slide 8 text

What is it? funky programming

Slide 9

Slide 9 text

What is it? no coupling of data and code with objects

Slide 10

Slide 10 text

What is it? only functions returng results

Slide 11

Slide 11 text

What is it? very high level of abstraction

Slide 12

Slide 12 text

What is it? programmable programming lang

Slide 13

Slide 13 text

Functional: programming for mathematicians

Slide 14

Slide 14 text

Functional: old school (58): LIS P , S c h e m e , λc alc ulus new school (70's): ML, Ocaml, Haskell

Slide 15

Slide 15 text

Started by:

Slide 16

Slide 16 text

Started by:

Slide 17

Slide 17 text

later:

Slide 18

Slide 18 text

Functional: old school: ty p e le ss new school: typed

Slide 19

Slide 19 text

Functional: old school: Perl 5 new school: Perl 6

Slide 20

Slide 20 text

2005

Slide 21

Slide 21 text

function >> sub sub sent { my $var = ... return $r; }

Slide 22

Slide 22 text

once “functional” if ($val eq '...'){ … }

Slide 23

Slide 23 text

recursion sub r { r($val) if … }

Slide 24

Slide 24 text

sub call { ... @_; } call ( @data ); variadic function

Slide 25

Slide 25 text

anonymous & first class return sub { … } return $coderef;

Slide 26

Slide 26 text

first class functional call( $coderef, @data);

Slide 27

Slide 27 text

high class functional sort $coderef @data;

Slide 28

Slide 28 text

High Order Func. sort map grep

Slide 29

Slide 29 text

only missing: macros

Slide 30

Slide 30 text

macro missing: Rolf Langsdorf is working on that

Slide 31

Slide 31 text

macro missing: github.com/LanX/Macro act.yapc.eu/lpw2014/talk/5804 yt/watch?v=i-Xz_9nFlvI

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

What is it? math is me mimo ... no side effects … mu ba ke

Slide 34

Slide 34 text

my => no side effects sub sent { my $var = ... return $r; }

Slide 35

Slide 35 text

procedural sub sent { if ($flag == …) { …} }

Slide 36

Slide 36 text

functional if ($flag == …) { eval “sub sent{...}”; }

Slide 37

Slide 37 text

What is it? programmable programming lang

Slide 38

Slide 38 text

What is it? very high level of abstraction

Slide 39

Slide 39 text

What is it? unified structure

Slide 40

Slide 40 text

functional world

Slide 41

Slide 41 text

Perl Philosophy more than 1 Way: programming for adults

Slide 42

Slide 42 text

Perl 5 Philosophy more than 1 Way: programming for assholekids

Slide 43

Slide 43 text

Perl 5 vs structure known mechanisms: signatures, types, objects

Slide 44

Slide 44 text

Perl 5.20: sub sent($s, $p, $o){ my $var = ... return $r; }

Slide 45

Slide 45 text

Perl 6: sub pwr(Int $b, Int $e {$^e < 22 }) { return $b ** $e; }

Slide 46

Slide 46 text

Perl 6: sub add(::T $a, T $b){ return $a + $b; }

Slide 47

Slide 47 text

Multi Method Dispatch multi sub ask (Num $p){ multi sub ask (Str $p){ ask (“what now?”);

Slide 48

Slide 48 text

Multi Method Dispatch $var ~~ … ;

Slide 49

Slide 49 text

Pattern Matching multi sub ask (Num $p){ multi sub ask (Str $p){ ask (“what now?”);

Slide 50

Slide 50 text

--< structure >++ constraint P5 <- P6 -> Has kell

Slide 51

Slide 51 text

Perl Philosophy more than 1 Way: programming for all

Slide 52

Slide 52 text

Perl: no prohibition

Slide 53

Slide 53 text

Perl: no prohibition

Slide 54

Slide 54 text

Perl: no prohibition

Slide 55

Slide 55 text

Perl: no prohibition changeable variables

Slide 56

Slide 56 text

Perl: no prohibition changeable variables sequential statements

Slide 57

Slide 57 text

Perl: no prohibition changeable variables sequential statements tweaking internals (OOP)

Slide 58

Slide 58 text

Perl: no prohibition changeable variables sequential statements tweaking internals (OOP) dirty macros (C, source filter)

Slide 59

Slide 59 text

Perl Philosophy make the hard possible: access to everything

Slide 60

Slide 60 text

Perl Philosophy keep the everyday simple: cut unnecessary syntax

Slide 61

Slide 61 text

keep it simple $text = slurp 'poem.txt';

Slide 62

Slide 62 text

keep it simple @vers = lines 'poem.txt'.IO;

Slide 63

Slide 63 text

keep it simple for @vers { ...

Slide 64

Slide 64 text

lazy eval for 'poem.txt'.IO.lines {

Slide 65

Slide 65 text

Iterators for (@a,$b,”i”) {…} say for <*>; File::find({...});

Slide 66

Slide 66 text

Iterators for @a,$b,”i” {…} say $_.path for ".".IO.dir

Slide 67

Slide 67 text

Iterators for 1..10 {…} for 100,81...4 {…}

Slide 68

Slide 68 text

Iterators for reverse 1..10 {…} for 100,81...4 {…} # 100 81 62 43 24 5

Slide 69

Slide 69 text

Iterators for reverse 1..10 {…} for 100,81,64...4 {…} # .. 49 36 25 16 9 4 ??

Slide 70

Slide 70 text

Iterators for reverse 1..10 {…} for 100,81,64...4 {…} # Error

Slide 71

Slide 71 text

Iterators for 100, {(sqrt($^n)–1)**2} … 4 { # 100 81 64 49 36 25 16 9 4

Slide 72

Slide 72 text

Iterators grep{$_>= 4}, map {$_**2}, reverse 0..10; # 100 81 64 49 36 25 16 9 4

Slide 73

Slide 73 text

Iterators grep{$_>= 4} <== map {$_**2} <== reverse 0..10; # 100 81 64 49 36 25 16 9 4

Slide 74

Slide 74 text

Iterators reverse 0..10 ==> map {$_**2} ==> grep{$_>= 4}; # 100 81 64 49 36 25 16 9 4

Slide 75

Slide 75 text

Iterators @r = gather {.. take ..}

Slide 76

Slide 76 text

Iterators .. … gather / take

Slide 77

Slide 77 text

Iterators no loop

Slide 78

Slide 78 text

Iterators no loop no changing var

Slide 79

Slide 79 text

Iterators no loop no changing var recursion

Slide 80

Slide 80 text

anonymous function @r = gather { ..take.. }

Slide 81

Slide 81 text

for @li Z @re -> $l,$r { parallel processing

Slide 82

Slide 82 text

for @l Z @m Z @r -> $l, $m, $r { ... parallel processing

Slide 83

Slide 83 text

for zip(@l;@m;@r) -> $l, $m, $r { ... parallel processing

Slide 84

Slide 84 text

Types perl6 -e 'say (2,3).WHAT' (Parcel) '@(2,3).WHAT' (List )

Slide 85

Slide 85 text

Types perl6 -e 'say "d".WHAT' (Str) 'my @a;say @a.WHAT' (Array)

Slide 86

Slide 86 text

Types in P6 elaborat tpe hierarchy for expressive signatures

Slide 87

Slide 87 text

Numbers whole number rational number complex number

Slide 88

Slide 88 text

Numbers '((1/3) + (1/6)).nude' 1 2 'say sqrt( -1+0i)' 6.12323399573677e-17+1i

Slide 89

Slide 89 text

Function in P5 Coderef with opaque CV-Structure

Slide 90

Slide 90 text

Function in P6 m e t a o b j e c t k n o w s : n r . & ( s u b - ) t y p e s o f p a r a m e t e r a n d r e t u r n v a l u e s

Slide 91

Slide 91 text

Metaops & Ops functions with visual names

Slide 92

Slide 92 text

Metaops [ ] [\ ] << >> R X Z =

Slide 93

Slide 93 text

Metaops [ ] [\ ] << >> X Z

Slide 94

Slide 94 text

Metaopmethods reduce() triangle() zipwith() crosswith()

Slide 95

Slide 95 text

my $min = $a min $b; Numeric Op

Slide 96

Slide 96 text

my $min = min @rray; New:

Slide 97

Slide 97 text

my $min = [min] @rray; previously:

Slide 98

Slide 98 text

my Bool $sorted = [ < ] @rray; sorted?

Slide 99

Slide 99 text

my $sum = [ + ] @rray; sum

Slide 100

Slide 100 text

my $sum =( @rray[0] + (@rray[1] + @rray[2] )); with 3 elements

Slide 101

Slide 101 text

my $sum =(( @rray[0] + @rray[1])+@rray[2]); with 3 elements

Slide 102

Slide 102 text

#associativity left my $sum = [ + ] @rray; # (((..+..) + ..) + ..) foldl / foldr

Slide 103

Slide 103 text

f o l d l : : ( a - > b - > a ) - > a - > [ b ] - > a f o l d l f z [ ] = z f o l d l f z ( x : x s ) = f o l d l f ( f z x ) x s foldl

Slide 104

Slide 104 text

sub add (Int $a, Int $b --> Int) {$a + $b}; sub foldl( &f:(Int, Int --> Int ), Int $sum, List:Int $l --> Int) { if $l.elems > 0 { my Int $first = shift $l; foldl(&f, &f($sum, $first), $l)} else {$sum} }; say foldl(&add, 0, [1..4]); foldl in P6

Slide 105

Slide 105 text

not there out of box easy to implement because nested signtures foldl in P6

Slide 106

Slide 106 text

#associativity left my $sum = [ + ] @rray; # (((..+..) + ..) + ..) foldl / foldr

Slide 107

Slide 107 text

#uses associativity of Op my $sum = [ + ] @rray; foldl / foldr

Slide 108

Slide 108 text

@rray.reduce(&[+]); boundary val

Slide 109

Slide 109 text

Slide 110

Slide 110 text

Slide 111

Slide 111 text

(<1 2>;<3 4>).zipwith(&[*]) <1 2> Z* <3 4> metaop Z

Slide 112

Slide 112 text

Hyper !!!

Slide 113

Slide 113 text

@age >>++; Birthday !!!

Slide 114

Slide 114 text

Birthday !!! @age >>+=>> 1;

Slide 115

Slide 115 text

@age == 18, 22, 35; @age = @age >>+>> 1; @age == 19, 23, 36; all age

Slide 116

Slide 116 text

@age == 18, 22, 35; @age = @age <<+<< 1; @age == 19; one ages

Slide 117

Slide 117 text

<18, 22, 35> >>+<< <1, 2> <18, 22, 35> <<+>> <1, 2> interesting cases

Slide 118

Slide 118 text

<18, 22, 35> >>+<< <1, 2> ERROR <18, 22, 35> <<+>> <1, 2> 19, 24, 36 interesting cases

Slide 119

Slide 119 text

async primitives promise / future channel, supply

Slide 120

Slide 120 text

Memoization sub expensive is cached { … }

Slide 121

Slide 121 text

Cheat Functional sub expensive is pure { … }

Slide 122

Slide 122 text

Currying applying one operand at a time

Slide 123

Slide 123 text

Currying f : X x Y --> Z ==> f : X --> ( Y --> Z )

Slide 124

Slide 124 text

Currying my &func := &substr.assuming('YAPC'); say func(0, 2); YA

Slide 125

Slide 125 text

hygienic Macro macro CompileTime(){ quasi { DateTime.now() }; }

Slide 126

Slide 126 text

hygienic is default macro is parsed Compile Time () { quasi { DateTime.now() }; }

Slide 127

Slide 127 text

dirty is possible macro is reparsed Comp T( { quasi { DateTime.now() }; }

Slide 128

Slide 128 text

Thank You