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
62
Perl 6 OOP
lichtkind
0
210
Perl 6 Regex
lichtkind
0
170
functional Perl 6
lichtkind
0
670
Other Decks in Programming
See All in Programming
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
nekko cloudにおけるProxmox VE利用事例
irumaru
3
420
たのしいparse.y
ydah
3
120
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
Security_for_introducing_eBPF
kentatada
0
110
103 Early Hints
sugi_0000
1
220
良いユニットテストを書こう
mototakatsu
5
1.9k
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
42 best practices for Symfony, a decade later
tucksaun
1
180
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
110
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Why Our Code Smells
bkeepers
PRO
335
57k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Embracing the Ebb and Flow
colly
84
4.5k
Designing Experiences People Love
moore
138
23k
4 Signs Your Business is Dying
shpigford
181
21k
Gamification - CAS2011
davidbonilla
80
5.1k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Speed Design
sergeychernyshev
25
670
Scaling GitHub
holman
458
140k
Building an army of robots
kneath
302
44k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
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