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 Golf
Search
ynonperek
April 09, 2012
Programming
4
1.8k
Perl Golf
An introduction to golfind
ynonperek
April 09, 2012
Tweet
Share
More Decks by ynonperek
See All by ynonperek
QtRuby for Qt Developers
ynonperek
0
230
Qt Hybrid Apps
ynonperek
1
210
QtRuby In Action
ynonperek
1
130
Cool CPAN Modules
ynonperek
2
590
Advanced Perl Moose
ynonperek
4
2.6k
Ruby Desktop Apps with Qt
ynonperek
1
540
git
ynonperek
3
780
Concurrency In Qt Applications
ynonperek
1
240
Moose Design Patterns
ynonperek
4
450
Other Decks in Programming
See All in Programming
現場から考えるソフトウェアエンジニアリングの価値と実験
nomuson
1
160
20241004 モノタロウ式~ドメインモデリングとリアーキテクチャ
monotaro
PRO
2
700
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
110
rtcamp 10 (vk-illuminati)
yumcyawiz
1
210
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
290
4年間変わらなかった YOUTRUSTのアーキテクチャ
daiki1003
2
710
CSC509 Lecture 06
javiergs
PRO
0
140
テスト駆動開発✅️
akitoshiga
1
180
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.1k
CSC305 Lecture 13
javiergs
PRO
0
110
Integrating AI in Your Enterprise Java Applications
ivargrimstad
0
950
Debugging: A Few Things You Should Know
jmatsu
1
140
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
The Power of CSS Pseudo Elements
geoffreycrofte
71
5.3k
Fireside Chat
paigeccino
32
3k
Scaling GitHub
holman
458
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
650
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Code Reviewing Like a Champion
maltzj
519
39k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
5
130
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
13
1.8k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Transcript
Let’s Golf Thursday, May 3, 12
About Me • Ynon Perek • http://ynonperek.com • http://speakerdeck.com/u/ ynonperek
Thursday, May 3, 12
The Game A Competition to find the shortest — fewest
keystrokes perl code to solve a given problem Thursday, May 3, 12
The Rules • Count keystrokes (newlines matter) • No modules
• No external tools Thursday, May 3, 12
Why Golf ? Thursday, May 3, 12
Example: Head use strict; use warnings; use v5.14; my $counter
= 0; while (<>) { $counter += 1; print; last if $counter > 10; } Thursday, May 3, 12
We Can Do Better while (<>) { print; last if
$. > 10; } Thursday, May 3, 12
Yet a golfer would • Remember perl opts: -pe •
Remember range operator Thursday, May 3, 12
Yet a golfer would #!/usr/bin/perl -pe last if $. >
10 Thursday, May 3, 12
Yet a golfer would perl -pe '11..exit' Thursday, May 3,
12
And a true golfer... perl -pe '11..&' Thursday, May 3,
12
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
Challenge • Use previous tricks to print: • characters count
• words count • line count Thursday, May 3, 12
Challenge # Characters count perl -lpe '$\+=y///c}{' # Words count
perl -lpe '$\+=split}{' # Lines count perl -pe '$\=$.}{' Thursday, May 3, 12
Example: tail • Tail is harder, because we need to
remember previous lines • Can use an array Thursday, May 3, 12
Example: tail # tail using an array perl -e '@a=<>;print
@a[-10..-1]' Thursday, May 3, 12
Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])'
Thursday, May 3, 12
Example: tail # Removing the array perl -e 'print ((<>)[-10..-1])'
Surprisingly, A true hacker can do better Thursday, May 3, 12
Example: tail # Recall the ~ perl -e 'print+(<>)[~9..-1]' Surprisingly,
A true hacker can do better Thursday, May 3, 12
Some More Tricks • $= Holds only int values. $==10/3
is the same as int(10/3) • $- Holds only positive values Thursday, May 3, 12
Example: $- perl -pe '$-=eval or&' 10+20 20+3 30-10 40-50
-5 1 4 5 Thursday, May 3, 12
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
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
Anagrams • Remove the sub • Prefer text over array-refs
• Prefer switches over code • Use short variable names Thursday, May 3, 12
Anagrams #!/usr/bin/perl -ln $w{ join '', sort split // }
.= "$_ "; }{ print for grep / ./, values %w; Thursday, May 3, 12
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
Anagrams #!/usr/bin/perl -ln $w{ 1, sort/./g } .= "$_ ";
}{/ ./&&print for %w; Thursday, May 3, 12
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
Non-perl Golf • http://vimgolf.com/ • http://codegolf.com/ Thursday, May 3, 12
What’s Next • Go to http:// terje2.frox25.no- ip.org/ • Download
the book Thursday, May 3, 12