Slide 1

Slide 1 text

Let’s Golf Thursday, May 3, 12

Slide 2

Slide 2 text

About Me • Ynon Perek • http://ynonperek.com • http://speakerdeck.com/u/ ynonperek Thursday, May 3, 12

Slide 3

Slide 3 text

The Game A Competition to find the shortest — fewest keystrokes perl code to solve a given problem Thursday, May 3, 12

Slide 4

Slide 4 text

The Rules • Count keystrokes (newlines matter) • No modules • No external tools Thursday, May 3, 12

Slide 5

Slide 5 text

Why Golf ? Thursday, May 3, 12

Slide 6

Slide 6 text

Example: Head use strict; use warnings; use v5.14; my $counter = 0; while (<>) { $counter += 1; print; last if $counter > 10; } Thursday, May 3, 12

Slide 7

Slide 7 text

We Can Do Better while (<>) { print; last if $. > 10; } Thursday, May 3, 12

Slide 8

Slide 8 text

Yet a golfer would • Remember perl opts: -pe • Remember range operator Thursday, May 3, 12

Slide 9

Slide 9 text

Yet a golfer would #!/usr/bin/perl -pe last if $. > 10 Thursday, May 3, 12

Slide 10

Slide 10 text

Yet a golfer would perl -pe '11..exit' Thursday, May 3, 12

Slide 11

Slide 11 text

And a true golfer... perl -pe '11..&' Thursday, May 3, 12

Slide 12

Slide 12 text

Some More Tricks • y///c Abigail’s length horror • $_ x= Larry’s boolean • }{ Abigail’s END for -pe • $\ Van-der Pijll print Thursday, May 3, 12

Slide 13

Slide 13 text

Challenge • Use previous tricks to print: • characters count • words count • line count Thursday, May 3, 12

Slide 14

Slide 14 text

Challenge # Characters count perl -lpe '$\+=y///c}{' # Words count perl -lpe '$\+=split}{' # Lines count perl -pe '$\=$.}{' Thursday, May 3, 12

Slide 15

Slide 15 text

Example: tail • Tail is harder, because we need to remember previous lines • Can use an array Thursday, May 3, 12

Slide 16

Slide 16 text

Example: tail # tail using an array perl -e '@a=<>;print @a[-10..-1]' Thursday, May 3, 12

Slide 17

Slide 17 text

Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])' Thursday, May 3, 12

Slide 18

Slide 18 text

Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])' Surprisingly, A true hacker can do better Thursday, May 3, 12

Slide 19

Slide 19 text

Example: tail # Recall the ~ perl -e 'print+(<>)[~9..-1]' Surprisingly, A true hacker can do better Thursday, May 3, 12

Slide 20

Slide 20 text

Some More Tricks • $= Holds only int values. $==10/3 is the same as int(10/3) • $- Holds only positive values Thursday, May 3, 12

Slide 21

Slide 21 text

Example: $- perl -pe '$-=eval or&' 10+20 20+3 30-10 40-50 -5 1 4 5 Thursday, May 3, 12

Slide 22

Slide 22 text

Golf Classics • Find all anagrams in a word list. • An anagram is two words made up of the same letters • Example: underflow - wonderful Thursday, May 3, 12

Slide 23

Slide 23 text

Anagrams #!/usr/bin/perl sub normalized { my ($word) = @_; my @letters = split //, $word; return join '', sort @letters; } while (<>) { chomp; my $letters = normalized( $_ ); $words{ $letters } ||= []; push $words{ $letters }, $_; } print "@$_\n" for grep { @$_ > 1} values %words; Thursday, May 3, 12

Slide 24

Slide 24 text

Anagrams • Remove the sub • Prefer text over array-refs • Prefer switches over code • Use short variable names Thursday, May 3, 12

Slide 25

Slide 25 text

Anagrams #!/usr/bin/perl -ln $w{ join '', sort split // } .= "$_ "; }{ print for grep / ./, values %w; Thursday, May 3, 12

Slide 26

Slide 26 text

Anagrams • No need to join hash keys • Can use regexp instead of split • Can use regexp instead of grep • Can use %h instead of values %h Thursday, May 3, 12

Slide 27

Slide 27 text

Anagrams #!/usr/bin/perl -ln $w{ 1, sort/./g } .= "$_ "; }{/ ./&&print for %w; Thursday, May 3, 12

Slide 28

Slide 28 text

Golf Classic sub b{[@b=(abs||No,bottle."s"x!!++$_,of,beer),on,the,wall]} print "@{+b},\n@b,\nTake one down, pass it around,\n@{+b}.\n" for-pop||-99..-1 99 bottles of beer on the wall, 99 bottles of beer, Take one down, pass it around, 98 bottles of beer on the wall. Thursday, May 3, 12

Slide 29

Slide 29 text

Non-perl Golf • http://vimgolf.com/ • http://codegolf.com/ Thursday, May 3, 12

Slide 30

Slide 30 text

What’s Next • Go to http:// terje2.frox25.no- ip.org/ • Download the book Thursday, May 3, 12