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. Perl 6 Data Structures

  2. Herbert Breunung

  3. CPAN: lichtkind

  4. Kephra 0.4.3.32

  5. Kephra Logo

  6. As Hieroglyph

  7. Mansfelder Land

  8. Mansfelder Land

  9. Egypt aka Khem

  10. Got A Camel

  11. Became Velociraptor

  12. Camelia

  13. Camelia

  14. None
  15. Cockaigne

  16. Holy Grail

  17. Beauty And Order

  18. Data Structures

  19. None
  20. @rrays And %ashes

  21. $calars Too

  22. Every Day Perl 5 @cities = ('roma', 'milano', 'turino');

  23. Perl 5 & 6 @cities = ('roma', 'milano', 'turino');

  24. Perl 5 @cities = qw(roma milano turino);

  25. Perl 6 @cities = qw(roma milano turino); @cities = <roma

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

    @cities = <roma milano turino>; @cities = <<roma milano $city>>;
  27. Perl 5 & 6 @cities = ('roma', 'milano', 'turino');

  28. Still P5 & P6 @cities = ('roma', 'milano', 'turino'); @cities

    = 'roma', 'milano', 'turino';
  29. Braces Now Just Group @cities = ('roma', 'milano', 'turino'); @cities

    = 'roma', 'milano', 'turino';
  30. Item Context $cities = ('roma', 'milano', 'turino'); $cities = 'roma',

    'milano', 'turino';
  31. Item Context # Parcel() $cities = ('roma', 'milano', 'turino'); #

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

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

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

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

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

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

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

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

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

    'milano', 'turino'; say $cities.WHAT # Array() say @cities.WHAT # Array()
  41. Number Of Elements say length @cities; # 3 say elems

    @cities; # 3 # no length
  42. elems Is A Method Too say length @cities; # 3

    say @cities.elems; # 3
  43. On A Scalar Too say scalar @$cities; # 3 say

    $cities.elems; # 3
  44. Content From Scalar say “@$cities“; # roma ... say $cities;

    # roma ...
  45. Scalar Context say “@cities“; # roma ... say $(@cities); #

    roma ...
  46. Scalar Context say “@cities“; # roma ... say item(@cities); #

    roma ...
  47. Numeric Context say scalar @cities; # 3 say + @cities;

    # 3
  48. Numeric Context say scalar @$cities; # 3 say + $cities;

    # 3
  49. Boolean Context say values @cities; # romami... say ? @cities;

    # Bool::True
  50. String Context say “@cities“; # roma milano say ~ @cities;

    # roma milano ...
  51. . Became ~ say “@cities“; # roma milano say “...“

    ~ @cities; # ...roma mila..
  52. “ “ Works in P6 Too say “@cities“;# roma milano

    turino say “@cities“;# roma milano turino?
  53. Not Quite say “@cities“; # roma milano ... say “@cities“;

    # @cities
  54. Add [] say “@cities“; # roma milano say “@cities[]“;# roma

    milano ...
  55. Was No Special Case say “@cities“; # roma milano say

    @cities[]; # roma milano ...
  56. Perl 5 & 6 say @cities; # romamilanoturino say @cities;

    # roma milano turino
  57. When Data Gets Deeper

  58. STD Perl 5 use Data::Dumper; say Dumper(@cities);

  59. $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
  60. $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
  61. He Fixed That use YAML; # Data::Dumper sucks say Dump

    @cities;
  62. --- - 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
  63. --- - 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
  64. Perl 6 say @cities.perl;

  65. Perl 6 # works to any depth say @cities.perl; $

    ["roma", "milano", "turino"]
  66. Perl 5 say $cities[2]; # turino

  67. Perl 6 say $cities[2]; # turino say @cities[2]; # turino

  68. Array In A Scalar say $cities[2]; # Perl 6 too

    say @cities[2]; # turino
  69. Same Container Type $cities = ['roma', 'milano', 'turino']; say $cities[2];

    # $cities.WHAT say @cities[2]; # @cities.WHAT
  70. Array Objects say $cities[2]; # Array() say @cities[2]; # Array()

  71. Say Is Just A Method say $cities[2]; # turino @cities[2].say;

    # too
  72. [ ] Too (Like Any Op) say $cities.[2]; # 2

    is a parameter @cities.[2].say; # too
  73. 2 == Last Index say $cities[2]; # turino say @cities[2];

    # too
  74. Perl 5 say $cities[-1]; # turino say @cities[2]; # too

  75. Perl 6 say $cities[-1]; # turino say @cities[*-1]; # too

  76. Whatever * say $cities[-1]; # turino say @cities[*-1]; # too

    #parameter – closure with 1 argument
  77. Whatever * say $cities[-1]; # turino say @cities[* / 2];#

    milano
  78. Whatever * say $cities[-1]; # turino say @cities[*]; # roma

    milano turino
  79. Whatever * say $cities[-1]; # turino say @cities[]; # roma

    milano turino # defaults to *
  80. LoL say @cities[1][1];

  81. List Of List @cities = [<roma ..>], [<berlin ..>]; say

    @cities[1][1]; say $cities[1;1];
  82. Let's Play A Game

  83. Count the Smurfs

  84. Count

  85. Count How many elements does this list have?

  86. Round 1

  87. How Many Elements ? my @number = 1, 2, 3,

    4;
  88. How Many Elements ? my @number = 1, 2, 3,

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

    4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4;
  90. 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;
  91. 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];
  92. 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 @_
  93. 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 & ;
  94. Round 2

  95. How Many Elements ? my $number = 1, 2, 3,

    4;
  96. How Many Elements ? my $number = 1, 2, 3,

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

    4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4;
  98. 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;
  99. 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);
  100. 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];
  101. 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
  102. Round 3

  103. How Many Elements ? my $pd = my @pd =

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

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

    (3, 1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4;
  106. 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;
  107. 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;
  108. 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;
  109. 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
  110. And The Winner Is ...

  111. Perl 5 @digits = ( 4 ) x 5; #

    (4,4,4,4,4)
  112. Perl 6 @digits = ( 4 ) x 5; #

    (4,4,4,4,4) @digits = 4 xx 5; # 4,4,4,4,4
  113. Multiply Strings @digits = 4 x 5; # '44444' @digits

    = 4 x 5; # '44444'
  114. Multiply Strings @digits = 4 x 5; # '44444' @digits

    = (4) x 5; # '44444'
  115. Multiply Strings @digits = '1 2' x 3; # '1

    21 21 2' @digits = (1,2) x 3; # '1 21 21 2'
  116. Multiply Lists @digits = (1,2) x 3; # 1,2,1,2,1,2 @digits

    = (1,2) xx 3; # 1,2,1,2,1,2
  117. Perl 5 @digits = ( 0 .. 9 ); #

    range op
  118. Perl 5 & 6 @digits = ( 0 .. 9

    );
  119. Perl 6 @digits = 0 .. 9;

  120. Perl 6 @digits = ^ 10; # 0 .. 9

  121. 0 Is Given @digits = 0 ..^ 10; # 0

    .. 9
  122. ^ Excludes The Range @digits = -1 ^..^ 10; #

    0 .. 9
  123. Sequence Operator @digits = 0 ... 9;

  124. Linear @digits = 0 ... 9; # 0 .. 9

  125. Arithmetic @digits = 0 ... 9; # 0 .. 9

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

    @odd = 1,3 … 9; # 1,3,5,7,9 @bin = 1,2,4 … 1024;#..,8,16,32,.
  127. 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,..
  128. Watch Your Limit @odd = 1,3 … 10; # 1,3,..

    *
  129. Infinite List @odd = 1,3 … 10; # 1,3,.. *

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

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

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

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

    Inf say $number.WHAT; # Range()
  134. Guess Output? $number = 0 .. *; # 0 ..

    Inf say $number; # ?
  135. First Element $number = 0 .. *; # 0 ..

    Inf say $number; # 0
  136. Infinite List $number = 0 .. *; # 0 ..

    Inf say @($number); # digits ...
  137. Array @number = 0 .. *; # 0 .. Inf

    say @number[5];
  138. Generates As Needed @number = 0 .. *; # 0

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

    Inf say @number.reify(5).perl; # (0, 1, 2, 3, 4, 5, 6..*)
  140. You Know These shift unshift push pop splice reverse map

    grep
  141. Perl 5 %opera = ( 'verdi' => 'Aida', 'puccini' =>

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

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

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

    'Turandot', 'rossini'=> 'Il barbiere', ; %opera.WHAT eq 'Hash()'
  145. New Pair Syntax %opera = :verdi('Aida'), :puccini('Turandot'), :rossini('Il barbiere'), ;

    %opera.WHAT eq 'Hash()'
  146. With Autoquoting %opera = :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, ; %opera.WHAT

    eq 'Hash()'
  147. Item Context $opera = :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, ; $opera.WHAT

    eq 'Pair()'
  148. With Grouping $opera = ( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, );

    %opera.WHAT eq 'Parcel()'
  149. Parcel Know Positions $opera = ( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,

    ); 0 verdi Aida 1 puccini Turandot 2 rossini Il barbiere
  150. Hash Context Op $opera = %( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,

    ); %opera.WHAT eq 'Hash()'
  151. Hash Context Op $opera = hash( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,

    ); %opera.WHAT eq 'Hash()'
  152. Hash Generator Op $opera = { :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,

    }; %opera.WHAT eq 'Hash()'
  153. Hash Generator Op %opera = { :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,

    }; %opera.WHAT eq 'Hash()'
  154. Invariant Sigils %opera{'rossini'}; # Il barbiere

  155. Invariant Sigils %opera.{'rossini'}; # alias .at_key()

  156. Remember qw() ? %opera{'rossini'}; # Il barbiere %opera<rossini>;

  157. With Interpolation %opera{'rossini'}; # Il barbiere %opera<rossini>; %opera<<$autore>>;

  158. Get 2 Operas %opera{'rossini', 'puccini'}; %opera<rossini puccini>; %opera<<@autore>>;

  159. Also Just Hashes $opera{'rossini', 'puccini'}; $opera<rossini puccini>; $opera<<@autore>>;

  160. Works On Parcel() Too $opera{'rossini', 'puccini'}; $opera<rossini puccini>; $opera<<@autore>>;

  161. Hash Methods %opera.keys; # verdi puccini ro... %opera.values; # aida

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

    roma milano ... @cities.kv; # 0 roma 1 milano 2 ...
  163. On Pairs $opera.key; # rossini $opera.value; # Il barbiere $opera.kv;

    #
  164. Other Hash Methods %opera.pairs; # list of pair obj. %opera.exists;

    # Bool %opera.delete; # 1 | 0
  165. Other Methods %opera.pairs; # list of pair obj. %opera.exists; #

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

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

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

    3 ~ %opera; # rossini Il barbiere
  169. New Hash Method say %opera.invert; Aida verdi Turandot puccini Il

    barbiere rossini
  170. Hash Generator my %h = @cities.classify ( { uc substr(

    $_, 0, 1 ) } ); say %h; $ R roma M milano T turino
  171. Hash Generator @cities.categorize({ catsub($_ ) }); say %h.perl;

  172. Hash Generator @cities.categorize({ water($_ ) }); say %h.perl; {"mare" =>

    ["roma"], "fiume" => ["roma", "milano", "turino"], "lago" => Nil}
  173. ==> Pipes

  174. Schwartz. Transform @results = map {…}, sort {…}, map {...},

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

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

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

    @input; @input ==> map {…} ==> sort {…} ==> map {…} ==> @results;
  178. Metaops

  179. Metaops = ! R

  180. Metaops >> [ ] [\ ] X Z S

  181. Metaops >> [ ] [\ ] X Z S

  182. Hyperoperator >>

  183. Hyperoperator >> .hyper()

  184. Simple Example (1..5) >>++

  185. That Works @number>>++

  186. Result @number>>++ 2, 3, 4, 5, 6

  187. More Complicated (1..5) >>+>> 1

  188. Check Your Spaces (1..5)_>>+>>_1

  189. Same Result (1..5) >>+>> 1 2, 3, 4, 5, 6

  190. Reverse Hyper (1..5) <<+<< 1

  191. Right Side Rules (1..5) <<+<< 1 2

  192. Both Rule ? (1..5) >>+<< 1

  193. Yes Both! (1..5) >>+<< 1 Runtime Error !!!

  194. Preserves LoL ? (1..5, [6..9]) >>+>> 1

  195. Preserves LoL ! (1..5, [6..9]) >>+>> 1 2, 3, 4,

    5, 6, [7, 8, 9,10]
  196. On 2 Sides ? (1..5, [6..9]) >>+<< (0..4, [1..4])

  197. On 2 Sides ! (1..5, [6..9]) >>+<< (0..4, [1..4]) (1,

    3, 5, 7, 9, [7, 9, 11, 13])
  198. How About This? (1..5, [6..9]) >>+<< (0..4, [1..5])

  199. Checks Lengths (1..5, [6..9]) >>+<< (0..4, [1..5]) Runtime Error

  200. Reduce [ ] .reduce

  201. Do The Gauss [+] (1..100)

  202. Do The Gauss [+] (1..100) 1 + 2 + 3

    + 4 + 5 + 6 + 7 + 8 + ...
  203. 100+1 * 50 [+] (1..100) 5050

  204. Chained Comparsion [<] (1..100)

  205. Chained Comparsion [<] (1..100) 1 < 2 < 3 <

    4 < 5 < 6 < 7 < 8 < ...
  206. Chained Comparsion [<] (1..100) Bool::True

  207. Junction all(1..100) > 0

  208. Junction all(1..100) 1 | 2 | 3 | 4 |

    5 | 6 | 7 | 8 | 9 | ...
  209. Junction all(1..100) > 0 (True, True, True,True, ...

  210. Bool Context ? all(1..100) > 0 True

  211. Bool Context ? all(1..100) > 1 False

  212. Metaops >> [ ] [\ ] X Z S

  213. Triangle [\ ] triangle()

  214. Back To Gauss [\+] (1..100)

  215. Returns A List [\+] (1..100) (((((1) + 2 ) +

    3) + 4) + 5) + 6) + ...
  216. Returns A List [\+] (1..100) (1, 3, 6, 10, 15,

    21, 28, ...
  217. Metaops >> [ ] [\ ] X Z S

  218. Cross .crosswith X

  219. Cross (1, 2) X (1, 2) 11, 12, 21, 22

  220. Cross With Power (1, 2) X** (1, 2)

  221. Cross With Power (1, 2) X** (1, 2) 1, 1,

    2, 4
  222. Metaops >> [ ] [\ ] X Z S

  223. Zip .zipwith() Z

  224. Zip (1..10) Z <a..j> <1a 2b 3c 4d 5e 6f

    ...>
  225. Zip for @a Z @b → $a, $b {

  226. Zip (1..4) Z+ (1..4)

  227. Zip (1..4) Z+ (1..4) 2, 4, 6, 8

  228. Metaops >> [ ] [\ ] X Z S

  229. Sequence .sequence S

  230. __END__ ?

  231. =begin END

  232. Thank You