Top Banner
Cool Things in Perl 6 brian d foy [email protected] May 3, 2008
32

Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy [email protected] May 3, 2008 • I'm not a Perl 6 contributor

May 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Cool Things in Perl 6

brian d [email protected]

May 3, 2008

Page 2: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• I'm not a Perl 6 contributor

• Not about the implementions

• Not about new syntax for old things

• About new features not in Perl 5

• Stuff that makes me want Perl 6

Page 3: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• Cribbed from the Synopseshttp://feather.perl6.nl/syn/

• Some of this might not work yet

Caveats

Page 4: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• Most languages can do the job

• But how much code does it take?

• And where does that code live?

• What's a primitive and what's built-in?

Stuff I want

Page 5: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• Junctions

• New list techniques

• Meta operators

In this talk

Page 6: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Junctions

Page 7: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• A junction is a single value that is equivalent to multiple values

• Useful with comparisons

• Parallelizable

• Short circuitable

Page 8: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Any• | or any()if $x == 1 | 2 | 3 { ... }

if $x eq any( q:w( a b c ) )

{...}

• Mutable(1 | 2 | 3 ) + 1; # 2 | 3 | 4

Page 9: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

All or one• & or all()if $x > ( $i & $j & $k ) {...}

if $x > all( $i, $j, $k ) {...}

• ^ or one()if $x == ($i ^ $j ^ $k) {...}

if $x == one($i, $j,$k) {...}

Page 10: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

none

• none()if $x eq none( $s, $t, $u )

{...}

Page 11: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Easy lists

Page 12: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Fancy ranges

• Lists can be unbounded0 .. *

• Not consecutive0 .. 100 :by(3)

Page 13: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Exclusive ranges• Exclusive lists1^..^10 # 2,3,4,5,6,7,8,9

• 0 up to one less^5 # 0,1,2,3,4

Page 14: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Multiple lists

• Zip lists to iterate over them togetherfor zip(@a, @b) -> $a, $b {

say "Got $a and $b" }

• Stops at shortest list

Page 15: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Feed operators

• Directs output to a "sink"@in ==> map {...} ==> @out

@out <== map {...} <== @in

• Source is lazy

• Allows parallelization

Page 16: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Multiple sources

• Stack multiple sources with ==>>

• Looks ahead for sinksource1() ==>>source2() ==>>source3() ==>>sink();

Page 17: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Meta operators

Page 18: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Superpowers

• Give normal operators super powers

• Make common operations even easier

• Remove messy looping monkey code

Page 19: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• Assignment

• Negated relational

• Hyper

• Reduction

• Cross

Five types

Page 20: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Assignment• Binary assignment like C and Perl 5

• Normal assignment$count = 5;

$count = $count + 1;

$count += 1;

• Mostly with scalar operators in Perl 5

Page 21: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

More operators• More operators (instead of builtins)

• The , operator to make a list@array = 1, 2, 3;

• Binary assignment is a push@array ,= 4, 5, 6

Page 22: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Negated relational• Put a ! in front of a comparatorif $version !== 6 { # or !=

say "How are we here?" }

if $version !> 5 { say "Here again?!" }

• Think "isn't greater than"

Page 23: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Hyperoperators• Obviates looping for single

operations

• Applies operation to each element@numbers >>++;

@negatives >>-;

• Can do either way@negatives = -<<@positives;

Page 24: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

• Surround an operator with angle brackets (no extra spaces)

>>op<< <<op>>

>>op>> <<op<<

• Makes new list

• Also with french quotes»op« «op» »op» «op«

List on list

Page 25: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

>>op<<

• List on the left and right

• One element from each for result(1,2,3) >>+<< (4,5,6) # 5,7,9

• Intersection of hash%foo >>+<< %bar

Page 26: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Hypergwimmery• Guess What I Mean (GWIM)

• Pointing one way GWIMs on that side

• One side is "shaped" differently(1,2,3) >>**>> 2 # 1,4,9

• Doesn't matter which side'.jpg' <<~<< q:w(a b) # a.jpg b.jpg

@numbers >>max>> 2

Page 27: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Doublegwimmery

• Which side needs shaping?

• Point all arrows outward

• Perl guesses@a <<+>> @b

Page 28: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Reduction

• Finally, a built-in reducemy $summerial = [+] @numbers;

my $factorial = [*] @numbers;

my $ascends = [<] @numbers;

Page 29: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Pseudo reduction

• Keep the intermediate results with \op[\+] ^4; # (0, 1, 3, 6);

• Produce a triangle list[\,] ^4

# ([0],[0,1],[0,1,2], [0,1,2,3]);

Page 30: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Cross operator

• Make tuples with Xq:w( a b ) X ( 1, 2 )

# (a, 1),(a,2),(b,1), (b,2)

Page 31: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Hypercross

• Perform the operation on all tuples(1,2) X~X q:w(a b)

# 1a, 1b, 2a, 2b

Page 32: Cool Things in Perl 6 · 2017-06-10 · Cool Things in Perl 6 brian d foy brian@stonehenge.com May 3, 2008 • I'm not a Perl 6 contributor

Questions