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

Perl 6 data structures

lichtkind
September 05, 2015

Perl 6 data structures

syntax (tidbits and cornder cases) of Perl 6 scalars, array and hashes

lichtkind

September 05, 2015
Tweet

More Decks by lichtkind

Other Decks in Programming

Transcript

  1. With Interpolation Like “ “ @cities = qw(roma milano turino);

    @cities = <roma milano turino>; @cities = <<roma milano $city>>;
  2. Item Context # Parcel() $cities = ('roma', 'milano', 'turino'); #

    Str() $cities = 'roma', 'milano', 'turino';
  3. What Is Str ? # Parcel() $cities = ('roma', 'milano',

    'turino'); # Str() - 'roma' is a string $cities = 'roma', 'milano', 'turino';
  4. Just the first value? # Parcel() $cities = ('roma', 'milano',

    'turino'); # $a, $b, $c = 1, 2, 3; $cities = 'roma', 'milano', 'turino';
  5. What is a Parcel ? # Parcel – type of

    a signiture $cities = ('roma', 'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
  6. () reminds you on what? # Parcel – like in

    function(1,2,3); $cities = ('roma', 'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
  7. Parcel # have positional and named args $cities = ('roma',

    'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
  8. Container Types # Array() $cities = ['roma', 'milano', 'turino']; #

    Parcel() $cities = ('roma', 'milano', 'turino');
  9. Array() ne List() ne Parcel() # List() $cities = @('roma',

    'milano', 'turino'); # Parcel() $cities = ('roma', 'milano', 'turino');
  10. Just A List # List() $cities = list('roma', 'milano', 'turino');

    # List() $cities = ||('roma', 'milano','turino');
  11. Array Object $cities = ['roma', 'milano', 'turino']; @cities = 'roma',

    'milano', 'turino'; say $cities.WHAT # Array() say @cities.WHAT # Array()
  12. “ “ Works in P6 Too say “@cities“;# roma milano

    turino say “@cities“;# roma milano turino?
  13. $VAR1 = 2; $VAR2 = 3; $VAR3 = 4; $VAR4

    = 5; $VAR5 = 6; $VAR6 = 7; $VAR7 = 7; $VAR8 = 2; $VAR9 = 5; $VAR10 = [ 'a', 'b', 'c' ]; [2, 3, 4, 5, 6, 7, 7, 2, 5, ["a", "b", "c"]] Data::Dumper Output
  14. $VAR1 = 2; $VAR2 = 3; $VAR3 = 4; $VAR4

    = 5; $VAR5 = 6; $VAR6 = 7; $VAR7 = 7; $VAR8 = 2; $VAR9 = 5; $VAR10 = [ 'a', 'b', 'c' ]; [2, 3, 4, 5, 6, 7, 7, 2, 5, ["a", "b", "c"]] Too Wordy
  15. --- - 2 - 3 - 4 - 5 -

    6 - 7 - 7 - 2 - 5 - - a - b - c [2, 3, 4, 5, 6, 7, 7, 2, 5, ["a", "b", "c"]] YAML Output
  16. --- - 2 - 3 - 4 - 5 -

    6 - 7 - 7 - 2 - 5 - - a - b - c [2, 3, 4, 5, 6, 7, 7, 2, 5, ["a", "b", "c"]] He Did It Shorter
  17. [ ] Too (Like Any Op) say $cities.[2]; # 2

    is a parameter @cities.[2].say; # too
  18. Whatever * say $cities[-1]; # turino say @cities[*-1]; # too

    #parameter – closure with 1 argument
  19. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4);
  20. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4;
  21. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4; # 4 my @number = 1, [2, 3], 4;
  22. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4; # 4 my @number = 1, [2, 3], 4; # 3 my @number = [1, 2, 3, 4];
  23. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4; # 4 my @number = 1, [2, 3], 4; # 3 my @number = [1, 2, 3, 4]; # 1 mysub 1,(2,[3, 4]), 5; # in @_
  24. How Many Elements ? my @number = 1, 2, 3,

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4; # 4 my @number = 1, [2, 3], 4; # 3 my @number = [1, 2, 3, 4]; # 1 mysub 1,(2,[3, 4]), 5; # 4 # all between name & ;
  25. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4);
  26. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4;
  27. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4; # 1 my $number = (1, 2), 3, 4;
  28. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4; # 1 my $number = (1, 2), 3, 4; # 2 my $number = (1, [2, 3], 4);
  29. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4; # 1 my $number = (1, 2), 3, 4; # 2 my $number = (1, [2, 3], 4); # 3 my $number = [1, 2, 3, 4];
  30. How Many Elements ? my $number = 1, 2, 3,

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4; # 1 my $number = (1, 2), 3, 4; # 2 my $number = (1, [2, 3], 4); # 3 my $number = [1, 2, 3, 4]; # 4
  31. How Many Elements ? my $pd = my @pd =

    (3, 1, 4, 1); @number = 1, @pd, 4;
  32. How Many Elements ? my $pd = my @pd =

    (3, 1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4;
  33. How Many Elements ? my $pd = my @pd =

    (3, 1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4; # 3 @number = 1, @pd.item, 4;
  34. item & list = Conext Ops my $pd = my

    @pd = (3, 1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4; # 3 @number = 1, @pd.item, 4; # 3 @number = 1, $pd.list, 4;
  35. Nil = Nothing my $pd = my @pd = (3,

    1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4; # 3 @number = 1, @pd.item, 4; # 3 @number = 1, $pd.list, 4; # 6 @number = Nil, Nil, Nil;
  36. Nil = Nothing my $pd = my @pd = (3,

    1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4; # 3 @number = 1, @pd.item, 4; # 3 @number = 1, $pd.list, 4; # 6 @number = Nil, Nil, Nil; # 0
  37. Perl 6 @digits = ( 4 ) x 5; #

    (4,4,4,4,4) @digits = 4 xx 5; # 4,4,4,4,4
  38. Multiply Strings @digits = '1 2' x 3; # '1

    21 21 2' @digits = (1,2) x 3; # '1 21 21 2'
  39. Arithmetic @digits = 0 ... 9; # 0 .. 9

    @odd = 1,3 … 9; # 1,3,5,7,9
  40. Geometric @digits = 0 ... 9; # 0 .. 9

    @odd = 1,3 … 9; # 1,3,5,7,9 @bin = 1,2,4 … 1024;#..,8,16,32,.
  41. Fibonacci @digits = 0 ... 9; # 0 .. 9

    @odd = 1,3 … 9; # 1,3,5,7,9 @bin = 1,2,4 … 1024;#..,8,16,32,. @fib = 0,1,*+* … 144; #..,1,2,3,..
  42. Infinite List @odd = 1,3 … 10; # 1,3,.. *

    @number = 0 .. *; # 0 .. Inf
  43. Certain Elements @odd = 1,3 … 10; # 1,3,.. *

    @number = 0 .. *; # 0 .. Inf say @odd[1]; # 3 say @number[5]; # 4
  44. All Elements @odd = 1,3 … 10; # 1,3,.. *

    @number = 0 .. *; # 0 .. Inf say @odd[*]; # waiting ... say @number; # lots of digits
  45. Number Of Elements @odd = 1,3 … 10; # 1,3,..

    * @number = 0 .. *; # 0 .. Inf say + @odd; # waiting ... say @number; # lots of digits
  46. What Happened? $number = 0 .. *; # 0 ..

    Inf say $number.WHAT; # Range()
  47. Infinite List $number = 0 .. *; # 0 ..

    Inf say @($number); # digits ...
  48. Generates As Needed @number = 0 .. *; # 0

    .. Inf say @number[5]; # @number.reify(5)
  49. Internal Representation @number = 0 .. *; # 0 ..

    Inf say @number.reify(5).perl; # (0, 1, 2, 3, 4, 5, 6..*)
  50. Perl 5 %opera = ( 'verdi' => 'Aida', 'puccini' =>

    'Turandot', 'rossini'=> 'Il barbiere', );
  51. Perl 6 %opera = ( 'verdi' => 'Aida', 'puccini' =>

    'Turandot', 'rossini'=> 'Il barbiere', );
  52. No Grouping Needed %opera = 'verdi' => 'Aida', 'puccini' =>

    'Turandot', 'rossini'=> 'Il barbiere', ;
  53. Still Hash Object %opera = 'verdi' => 'Aida', 'puccini' =>

    'Turandot', 'rossini'=> 'Il barbiere', ; %opera.WHAT eq 'Hash()'
  54. Hash Methods %opera.keys; # verdi puccini ro... %opera.values; # aida

    turandot ... %opera.kv; # verdi aida puccini ...
  55. On Arrays Too @cities.keys; # 0 1 2 @cities.values; #

    roma milano ... @cities.kv; # 0 roma 1 milano 2 ...
  56. Other Methods %opera.pairs; # list of pair obj. %opera.exists; #

    one at a time %opera.delete; # one at a time
  57. Hash In Context ? %opera; # bool context + %opera;

    # num context ~ %opera; # string context
  58. Hash In Context ? %opera; # Bool::True + %opera; #

    3 ~ %opera; # rossini Il barbiere
  59. Value In Context ? %opera; # Bool::True + %opera; #

    3 ~ %opera; # rossini Il barbiere
  60. Hash Generator my %h = @cities.classify ( { uc substr(

    $_, 0, 1 ) } ); say %h; $ R roma M milano T turino
  61. Hash Generator @cities.categorize({ water($_ ) }); say %h.perl; {"mare" =>

    ["roma"], "fiume" => ["roma", "milano", "turino"], "lago" => Nil}
  62. Schwartz. Transform @results = map {…}, sort {…}, map {...},

    @input; @results = map {…} <== sort {…} <== map {…} <== @input;
  63. Schwartz. Transform @results = map {…}, sort {…}, map {...},

    @input; @results <== map {…} <== sort {…} <== map {…} <== @input;
  64. Schwartz. Transform @results = map {…}, sort {…}, map {...},

    @input; @input ==> map {…} ==> sort {…} ==> map {…} ==> @results;
  65. Do The Gauss [+] (1..100) 1 + 2 + 3

    + 4 + 5 + 6 + 7 + 8 + ...
  66. Junction all(1..100) 1 | 2 | 3 | 4 |

    5 | 6 | 7 | 8 | 9 | ...