Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Perl 6 data structures
Search
lichtkind
September 05, 2015
Programming
0
110
Perl 6 data structures
syntax (tidbits and cornder cases) of Perl 6 scalars, array and hashes
lichtkind
September 05, 2015
Tweet
Share
More Decks by lichtkind
See All by lichtkind
Perl 6 in context
lichtkind
0
64
Perl 6 OOP
lichtkind
0
210
Perl 6 Regex
lichtkind
0
170
functional Perl 6
lichtkind
0
680
Other Decks in Programming
See All in Programming
QA環境で誰でも自由自在に現在時刻を操って検証できるようにした話
kalibora
1
140
快速入門可觀測性
blueswen
0
500
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
930
.NETでOBS Studio操作してみたけど…… / Operating OBS Studio by .NET
skasweb
0
120
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
290
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
150
ErdMap: Thinking about a map for Rails applications
makicamel
1
570
Azure AI Foundryのご紹介
qt_luigi
1
160
2025.01.17_Sansan × DMM.swift
riofujimon
2
530
php-conference-japan-2024
tasuku43
0
430
情報漏洩させないための設計
kubotak
5
1.3k
Package Traits
ikesyo
1
200
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
KATA
mclloyd
29
14k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Unsuck your backbone
ammeep
669
57k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
YesSQL, Process and Tooling at Scale
rocio
170
14k
Transcript
Perl 6 Data Structures
Herbert Breunung
CPAN: lichtkind
Kephra 0.4.3.32
Kephra Logo
As Hieroglyph
Mansfelder Land
Mansfelder Land
Egypt aka Khem
Got A Camel
Became Velociraptor
Camelia
Camelia
None
Cockaigne
Holy Grail
Beauty And Order
Data Structures
None
@rrays And %ashes
$calars Too
Every Day Perl 5 @cities = ('roma', 'milano', 'turino');
Perl 5 & 6 @cities = ('roma', 'milano', 'turino');
Perl 5 @cities = qw(roma milano turino);
Perl 6 @cities = qw(roma milano turino); @cities = <roma
milano turino>;
With Interpolation Like “ “ @cities = qw(roma milano turino);
@cities = <roma milano turino>; @cities = <<roma milano $city>>;
Perl 5 & 6 @cities = ('roma', 'milano', 'turino');
Still P5 & P6 @cities = ('roma', 'milano', 'turino'); @cities
= 'roma', 'milano', 'turino';
Braces Now Just Group @cities = ('roma', 'milano', 'turino'); @cities
= 'roma', 'milano', 'turino';
Item Context $cities = ('roma', 'milano', 'turino'); $cities = 'roma',
'milano', 'turino';
Item Context # Parcel() $cities = ('roma', 'milano', 'turino'); #
Str() $cities = 'roma', 'milano', 'turino';
What Is Str ? # Parcel() $cities = ('roma', 'milano',
'turino'); # Str() - 'roma' is a string $cities = 'roma', 'milano', 'turino';
Just the first value? # Parcel() $cities = ('roma', 'milano',
'turino'); # $a, $b, $c = 1, 2, 3; $cities = 'roma', 'milano', 'turino';
What is a Parcel ? # Parcel – type of
a signiture $cities = ('roma', 'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
() reminds you on what? # Parcel – like in
function(1,2,3); $cities = ('roma', 'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
Parcel # have positional and named args $cities = ('roma',
'milano', 'turino'); # Str() $cities = 'roma', 'milano', 'turino';
Container Types # Array() $cities = ['roma', 'milano', 'turino']; #
Parcel() $cities = ('roma', 'milano', 'turino');
Array() ne List() ne Parcel() # List() $cities = @('roma',
'milano', 'turino'); # Parcel() $cities = ('roma', 'milano', 'turino');
Just A List # List() $cities = list('roma', 'milano', 'turino');
# List() $cities = ||('roma', 'milano','turino');
Array Object $cities = ['roma', 'milano', 'turino']; @cities = 'roma',
'milano', 'turino'; say $cities.WHAT # Array() say @cities.WHAT # Array()
Number Of Elements say length @cities; # 3 say elems
@cities; # 3 # no length
elems Is A Method Too say length @cities; # 3
say @cities.elems; # 3
On A Scalar Too say scalar @$cities; # 3 say
$cities.elems; # 3
Content From Scalar say “@$cities“; # roma ... say $cities;
# roma ...
Scalar Context say “@cities“; # roma ... say $(@cities); #
roma ...
Scalar Context say “@cities“; # roma ... say item(@cities); #
roma ...
Numeric Context say scalar @cities; # 3 say + @cities;
# 3
Numeric Context say scalar @$cities; # 3 say + $cities;
# 3
Boolean Context say values @cities; # romami... say ? @cities;
# Bool::True
String Context say “@cities“; # roma milano say ~ @cities;
# roma milano ...
. Became ~ say “@cities“; # roma milano say “...“
~ @cities; # ...roma mila..
“ “ Works in P6 Too say “@cities“;# roma milano
turino say “@cities“;# roma milano turino?
Not Quite say “@cities“; # roma milano ... say “@cities“;
# @cities
Add [] say “@cities“; # roma milano say “@cities[]“;# roma
milano ...
Was No Special Case say “@cities“; # roma milano say
@cities[]; # roma milano ...
Perl 5 & 6 say @cities; # romamilanoturino say @cities;
# roma milano turino
When Data Gets Deeper
STD Perl 5 use Data::Dumper; say Dumper(@cities);
$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
$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
He Fixed That use YAML; # Data::Dumper sucks say Dump
@cities;
--- - 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
--- - 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
Perl 6 say @cities.perl;
Perl 6 # works to any depth say @cities.perl; $
["roma", "milano", "turino"]
Perl 5 say $cities[2]; # turino
Perl 6 say $cities[2]; # turino say @cities[2]; # turino
Array In A Scalar say $cities[2]; # Perl 6 too
say @cities[2]; # turino
Same Container Type $cities = ['roma', 'milano', 'turino']; say $cities[2];
# $cities.WHAT say @cities[2]; # @cities.WHAT
Array Objects say $cities[2]; # Array() say @cities[2]; # Array()
Say Is Just A Method say $cities[2]; # turino @cities[2].say;
# too
[ ] Too (Like Any Op) say $cities.[2]; # 2
is a parameter @cities.[2].say; # too
2 == Last Index say $cities[2]; # turino say @cities[2];
# too
Perl 5 say $cities[-1]; # turino say @cities[2]; # too
Perl 6 say $cities[-1]; # turino say @cities[*-1]; # too
Whatever * say $cities[-1]; # turino say @cities[*-1]; # too
#parameter – closure with 1 argument
Whatever * say $cities[-1]; # turino say @cities[* / 2];#
milano
Whatever * say $cities[-1]; # turino say @cities[*]; # roma
milano turino
Whatever * say $cities[-1]; # turino say @cities[]; # roma
milano turino # defaults to *
LoL say @cities[1][1];
List Of List @cities = [<roma ..>], [<berlin ..>]; say
@cities[1][1]; say $cities[1;1];
Let's Play A Game
Count the Smurfs
Count
Count How many elements does this list have?
Round 1
How Many Elements ? my @number = 1, 2, 3,
4;
How Many Elements ? my @number = 1, 2, 3,
4; # 4 my @number = (1, 2, 3, 4);
How Many Elements ? my @number = 1, 2, 3,
4; # 4 my @number = (1, 2, 3, 4); # 4 my @number = 1, (2, 3), 4;
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;
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];
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 @_
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 & ;
Round 2
How Many Elements ? my $number = 1, 2, 3,
4;
How Many Elements ? my $number = 1, 2, 3,
4; # 1 my $number = (1, 2, 3, 4);
How Many Elements ? my $number = 1, 2, 3,
4; # 1 my $number = (1, 2, 3, 4); # 4 my $number = 1, (2, 3), 4;
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;
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);
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];
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
Round 3
How Many Elements ? my $pd = my @pd =
(3, 1, 4, 1);
How Many Elements ? my $pd = my @pd =
(3, 1, 4, 1); @number = 1, @pd, 4;
How Many Elements ? my $pd = my @pd =
(3, 1, 4, 1); @number = 1, @pd, 4; # 6 @number = 1, $pd, 4;
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;
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;
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;
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
And The Winner Is ...
Perl 5 @digits = ( 4 ) x 5; #
(4,4,4,4,4)
Perl 6 @digits = ( 4 ) x 5; #
(4,4,4,4,4) @digits = 4 xx 5; # 4,4,4,4,4
Multiply Strings @digits = 4 x 5; # '44444' @digits
= 4 x 5; # '44444'
Multiply Strings @digits = 4 x 5; # '44444' @digits
= (4) x 5; # '44444'
Multiply Strings @digits = '1 2' x 3; # '1
21 21 2' @digits = (1,2) x 3; # '1 21 21 2'
Multiply Lists @digits = (1,2) x 3; # 1,2,1,2,1,2 @digits
= (1,2) xx 3; # 1,2,1,2,1,2
Perl 5 @digits = ( 0 .. 9 ); #
range op
Perl 5 & 6 @digits = ( 0 .. 9
);
Perl 6 @digits = 0 .. 9;
Perl 6 @digits = ^ 10; # 0 .. 9
0 Is Given @digits = 0 ..^ 10; # 0
.. 9
^ Excludes The Range @digits = -1 ^..^ 10; #
0 .. 9
Sequence Operator @digits = 0 ... 9;
Linear @digits = 0 ... 9; # 0 .. 9
Arithmetic @digits = 0 ... 9; # 0 .. 9
@odd = 1,3 … 9; # 1,3,5,7,9
Geometric @digits = 0 ... 9; # 0 .. 9
@odd = 1,3 … 9; # 1,3,5,7,9 @bin = 1,2,4 … 1024;#..,8,16,32,.
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,..
Watch Your Limit @odd = 1,3 … 10; # 1,3,..
*
Infinite List @odd = 1,3 … 10; # 1,3,.. *
@number = 0 .. *; # 0 .. Inf
Certain Elements @odd = 1,3 … 10; # 1,3,.. *
@number = 0 .. *; # 0 .. Inf say @odd[1]; # 3 say @number[5]; # 4
All Elements @odd = 1,3 … 10; # 1,3,.. *
@number = 0 .. *; # 0 .. Inf say @odd[*]; # waiting ... say @number; # lots of digits
Number Of Elements @odd = 1,3 … 10; # 1,3,..
* @number = 0 .. *; # 0 .. Inf say + @odd; # waiting ... say @number; # lots of digits
What Happened? $number = 0 .. *; # 0 ..
Inf say $number.WHAT; # Range()
Guess Output? $number = 0 .. *; # 0 ..
Inf say $number; # ?
First Element $number = 0 .. *; # 0 ..
Inf say $number; # 0
Infinite List $number = 0 .. *; # 0 ..
Inf say @($number); # digits ...
Array @number = 0 .. *; # 0 .. Inf
say @number[5];
Generates As Needed @number = 0 .. *; # 0
.. Inf say @number[5]; # @number.reify(5)
Internal Representation @number = 0 .. *; # 0 ..
Inf say @number.reify(5).perl; # (0, 1, 2, 3, 4, 5, 6..*)
You Know These shift unshift push pop splice reverse map
grep
Perl 5 %opera = ( 'verdi' => 'Aida', 'puccini' =>
'Turandot', 'rossini'=> 'Il barbiere', );
Perl 6 %opera = ( 'verdi' => 'Aida', 'puccini' =>
'Turandot', 'rossini'=> 'Il barbiere', );
No Grouping Needed %opera = 'verdi' => 'Aida', 'puccini' =>
'Turandot', 'rossini'=> 'Il barbiere', ;
Still Hash Object %opera = 'verdi' => 'Aida', 'puccini' =>
'Turandot', 'rossini'=> 'Il barbiere', ; %opera.WHAT eq 'Hash()'
New Pair Syntax %opera = :verdi('Aida'), :puccini('Turandot'), :rossini('Il barbiere'), ;
%opera.WHAT eq 'Hash()'
With Autoquoting %opera = :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, ; %opera.WHAT
eq 'Hash()'
Item Context $opera = :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, ; $opera.WHAT
eq 'Pair()'
With Grouping $opera = ( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>, );
%opera.WHAT eq 'Parcel()'
Parcel Know Positions $opera = ( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,
); 0 verdi Aida 1 puccini Turandot 2 rossini Il barbiere
Hash Context Op $opera = %( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,
); %opera.WHAT eq 'Hash()'
Hash Context Op $opera = hash( :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,
); %opera.WHAT eq 'Hash()'
Hash Generator Op $opera = { :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,
}; %opera.WHAT eq 'Hash()'
Hash Generator Op %opera = { :verdi<Aida>, :puccini<Turandot>, :rossini<Il barbiere>,
}; %opera.WHAT eq 'Hash()'
Invariant Sigils %opera{'rossini'}; # Il barbiere
Invariant Sigils %opera.{'rossini'}; # alias .at_key()
Remember qw() ? %opera{'rossini'}; # Il barbiere %opera<rossini>;
With Interpolation %opera{'rossini'}; # Il barbiere %opera<rossini>; %opera<<$autore>>;
Get 2 Operas %opera{'rossini', 'puccini'}; %opera<rossini puccini>; %opera<<@autore>>;
Also Just Hashes $opera{'rossini', 'puccini'}; $opera<rossini puccini>; $opera<<@autore>>;
Works On Parcel() Too $opera{'rossini', 'puccini'}; $opera<rossini puccini>; $opera<<@autore>>;
Hash Methods %opera.keys; # verdi puccini ro... %opera.values; # aida
turandot ... %opera.kv; # verdi aida puccini ...
On Arrays Too @cities.keys; # 0 1 2 @cities.values; #
roma milano ... @cities.kv; # 0 roma 1 milano 2 ...
On Pairs $opera.key; # rossini $opera.value; # Il barbiere $opera.kv;
#
Other Hash Methods %opera.pairs; # list of pair obj. %opera.exists;
# Bool %opera.delete; # 1 | 0
Other Methods %opera.pairs; # list of pair obj. %opera.exists; #
one at a time %opera.delete; # one at a time
Hash In Context ? %opera; # bool context + %opera;
# num context ~ %opera; # string context
Hash In Context ? %opera; # Bool::True + %opera; #
3 ~ %opera; # rossini Il barbiere
Value In Context ? %opera; # Bool::True + %opera; #
3 ~ %opera; # rossini Il barbiere
New Hash Method say %opera.invert; Aida verdi Turandot puccini Il
barbiere rossini
Hash Generator my %h = @cities.classify ( { uc substr(
$_, 0, 1 ) } ); say %h; $ R roma M milano T turino
Hash Generator @cities.categorize({ catsub($_ ) }); say %h.perl;
Hash Generator @cities.categorize({ water($_ ) }); say %h.perl; {"mare" =>
["roma"], "fiume" => ["roma", "milano", "turino"], "lago" => Nil}
==> Pipes
Schwartz. Transform @results = map {…}, sort {…}, map {...},
@input;
Schwartz. Transform @results = map {…}, sort {…}, map {...},
@input; @results = map {…} <== sort {…} <== map {…} <== @input;
Schwartz. Transform @results = map {…}, sort {…}, map {...},
@input; @results <== map {…} <== sort {…} <== map {…} <== @input;
Schwartz. Transform @results = map {…}, sort {…}, map {...},
@input; @input ==> map {…} ==> sort {…} ==> map {…} ==> @results;
Metaops
Metaops = ! R
Metaops >> [ ] [\ ] X Z S
Metaops >> [ ] [\ ] X Z S
Hyperoperator >>
Hyperoperator >> .hyper()
Simple Example (1..5) >>++
That Works @number>>++
Result @number>>++ 2, 3, 4, 5, 6
More Complicated (1..5) >>+>> 1
Check Your Spaces (1..5)_>>+>>_1
Same Result (1..5) >>+>> 1 2, 3, 4, 5, 6
Reverse Hyper (1..5) <<+<< 1
Right Side Rules (1..5) <<+<< 1 2
Both Rule ? (1..5) >>+<< 1
Yes Both! (1..5) >>+<< 1 Runtime Error !!!
Preserves LoL ? (1..5, [6..9]) >>+>> 1
Preserves LoL ! (1..5, [6..9]) >>+>> 1 2, 3, 4,
5, 6, [7, 8, 9,10]
On 2 Sides ? (1..5, [6..9]) >>+<< (0..4, [1..4])
On 2 Sides ! (1..5, [6..9]) >>+<< (0..4, [1..4]) (1,
3, 5, 7, 9, [7, 9, 11, 13])
How About This? (1..5, [6..9]) >>+<< (0..4, [1..5])
Checks Lengths (1..5, [6..9]) >>+<< (0..4, [1..5]) Runtime Error
Reduce [ ] .reduce
Do The Gauss [+] (1..100)
Do The Gauss [+] (1..100) 1 + 2 + 3
+ 4 + 5 + 6 + 7 + 8 + ...
100+1 * 50 [+] (1..100) 5050
Chained Comparsion [<] (1..100)
Chained Comparsion [<] (1..100) 1 < 2 < 3 <
4 < 5 < 6 < 7 < 8 < ...
Chained Comparsion [<] (1..100) Bool::True
Junction all(1..100) > 0
Junction all(1..100) 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | ...
Junction all(1..100) > 0 (True, True, True,True, ...
Bool Context ? all(1..100) > 0 True
Bool Context ? all(1..100) > 1 False
Metaops >> [ ] [\ ] X Z S
Triangle [\ ] triangle()
Back To Gauss [\+] (1..100)
Returns A List [\+] (1..100) (((((1) + 2 ) +
3) + 4) + 5) + 6) + ...
Returns A List [\+] (1..100) (1, 3, 6, 10, 15,
21, 28, ...
Metaops >> [ ] [\ ] X Z S
Cross .crosswith X
Cross (1, 2) X (1, 2) 11, 12, 21, 22
Cross With Power (1, 2) X** (1, 2)
Cross With Power (1, 2) X** (1, 2) 1, 1,
2, 4
Metaops >> [ ] [\ ] X Z S
Zip .zipwith() Z
Zip (1..10) Z <a..j> <1a 2b 3c 4d 5e 6f
...>
Zip for @a Z @b → $a, $b {
Zip (1..4) Z+ (1..4)
Zip (1..4) Z+ (1..4) 2, 4, 6, 8
Metaops >> [ ] [\ ] X Z S
Sequence .sequence S
__END__ ?
=begin END
Thank You