Upgrade to Pro — share decks privately, control downloads, hide ads and more …

functional Perl 6

Cac1159892ad59610a9ed68685fd0c1c?s=47 lichtkind
September 04, 2015

functional Perl 6

important functional features Haskell has, that are supported by Perl 6 too and the cultural differences between Perl and functional

Cac1159892ad59610a9ed68685fd0c1c?s=128

lichtkind

September 04, 2015
Tweet

Transcript

  1. Perl 6 functional

  2. Previously: general Array / Hash Regex OOP operator

  3. func. programming What is it? What is it good for?

    How much can P6? How it's done in P6?
  4. Features: parameter, functions, HOP (abstract) types, -classes, -inferenz lists-, comprehension,

    generators, tuple, currying,folding,map,filter pattern matching (MMD)
  5. Funky Features: We have almost all!

  6. Funky Features: We have almost all!

  7. Perl Philosophy moe than 1 Way: programming for adults

  8. What is it? funky programming

  9. What is it? no coupling of data and code with

    objects
  10. What is it? only functions returng results

  11. What is it? very high level of abstraction

  12. What is it? programmable programming lang

  13. Functional: programming for mathematicians

  14. Functional: old school (58): LIS P , S c h

    e m e , λc alc ulus new school (70's): ML, Ocaml, Haskell
  15. Started by:

  16. Started by:

  17. later:

  18. Functional: old school: ty p e le ss new school:

    typed
  19. Functional: old school: Perl 5 new school: Perl 6

  20. 2005

  21. function >> sub sub sent { my $var = ...

    return $r; }
  22. once “functional” if ($val eq '...'){ … }

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

  24. sub call { ... @_; } call ( @data );

    variadic function
  25. anonymous & first class return sub { … } return

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

  27. high class functional sort $coderef @data;

  28. High Order Func. sort map grep

  29. only missing: macros

  30. macro missing: Rolf Langsdorf is working on that

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

  32. None
  33. What is it? math is me mimo ... no side

    effects … mu ba ke
  34. my => no side effects sub sent { my $var

    = ... return $r; }
  35. procedural sub sent { if ($flag == …) { …}

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

  37. What is it? programmable programming lang

  38. What is it? very high level of abstraction

  39. What is it? unified structure

  40. functional world

  41. Perl Philosophy more than 1 Way: programming for adults

  42. Perl 5 Philosophy more than 1 Way: programming for assholekids

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

  44. Perl 5.20: sub sent($s, $p, $o){ my $var = ...

    return $r; }
  45. Perl 6: sub pwr(Int $b, Int $e {$^e < 22

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

    $b; }
  47. Multi Method Dispatch multi sub ask (Num $p){ multi sub

    ask (Str $p){ ask (“what now?”);
  48. Multi Method Dispatch $var ~~ … ;

  49. Pattern Matching multi sub ask (Num $p){ multi sub ask

    (Str $p){ ask (“what now?”);
  50. --< structure >++ constraint P5 <- P6 -> Has kell

  51. Perl Philosophy more than 1 Way: programming for all

  52. Perl: no prohibition

  53. Perl: no prohibition

  54. Perl: no prohibition

  55. Perl: no prohibition changeable variables

  56. Perl: no prohibition changeable variables sequential statements

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

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

    dirty macros (C, source filter)
  59. Perl Philosophy make the hard possible: access to everything

  60. Perl Philosophy keep the everyday simple: cut unnecessary syntax

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

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

  63. keep it simple for @vers { ...

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

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

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

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

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

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

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

  71. Iterators for 100, {(sqrt($^n)–1)**2} … 4 { # 100 81

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

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

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

    100 81 64 49 36 25 16 9 4
  75. Iterators @r = gather {.. take ..}

  76. Iterators .. … gather / take

  77. Iterators no loop

  78. Iterators no loop no changing var

  79. Iterators no loop no changing var recursion

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

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

  82. for @l Z @m Z @r -> $l, $m, $r

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

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

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

  86. Types in P6 elaborat tpe hierarchy for expressive signatures

  87. Numbers whole number rational number complex number

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

  89. Function in P5 Coderef with opaque CV-Structure

  90. 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
  91. Metaops & Ops functions with visual names

  92. Metaops [ ] [\ ] << >> R X Z

    =
  93. Metaops [ ] [\ ] << >> X Z

  94. Metaopmethods reduce() triangle() zipwith() crosswith()

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

  96. my $min = min @rray; New:

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

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

  99. my $sum = [ + ] @rray; sum

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

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

  102. #associativity left my $sum = [ + ] @rray; #

    (((..+..) + ..) + ..) foldl / foldr
  103. 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
  104. 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
  105. not there out of box easy to implement because nested

    signtures foldl in P6
  106. #associativity left my $sum = [ + ] @rray; #

    (((..+..) + ..) + ..) foldl / foldr
  107. #uses associativity of Op my $sum = [ + ]

    @rray; foldl / foldr
  108. @rray.reduce(&[+]); boundary val

  109. <1 2> X~ <a b> '1a','1b','2a','2b' cartesian pairs

  110. <1 2> Z~ <a b> '1a','1b','2a','2b' zip pairs

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

  112. Hyper !!!

  113. @age >>++; Birthday !!!

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

  115. @age == 18, 22, 35; @age = @age >>+>> 1;

    @age == 19, 23, 36; all age
  116. @age == 18, 22, 35; @age = @age <<+<< 1;

    @age == 19; one ages
  117. <18, 22, 35> >>+<< <1, 2> <18, 22, 35> <<+>>

    <1, 2> interesting cases
  118. <18, 22, 35> >>+<< <1, 2> ERROR <18, 22, 35>

    <<+>> <1, 2> 19, 24, 36 interesting cases
  119. async primitives promise / future channel, supply

  120. Memoization sub expensive is cached { … }

  121. Cheat Functional sub expensive is pure { … }

  122. Currying applying one operand at a time

  123. Currying f : X x Y --> Z ==> f

    : X --> ( Y --> Z )
  124. Currying my &func := &substr.assuming('YAPC'); say func(0, 2); YA

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

  126. hygienic is default macro is parsed Compile Time () {

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

    { DateTime.now() }; }
  128. Thank You