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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ynonperek
April 09, 2012
Programming
2.2k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Perl Golf
An introduction to golfind
ynonperek
April 09, 2012
More Decks by ynonperek
See All by ynonperek
QtRuby for Qt Developers
ynonperek
0
340
Qt Hybrid Apps
ynonperek
1
320
QtRuby In Action
ynonperek
1
190
Cool CPAN Modules
ynonperek
2
650
Advanced Perl Moose
ynonperek
4
2.7k
Ruby Desktop Apps with Qt
ynonperek
1
650
git
ynonperek
3
850
Concurrency In Qt Applications
ynonperek
1
390
Moose Design Patterns
ynonperek
4
630
Other Decks in Programming
See All in Programming
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
130
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
560
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
AI 輔助遺留系統現代化的經驗分享
jame2408
1
950
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
580
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.2k
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.3k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
270
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
130
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.5k
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
340
Documentation Writing (for coders)
carmenintech
77
5.4k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
170
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Abbi's Birthday
coloredviolet
3
8.2k
A Tale of Four Properties
chriscoyier
163
24k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
320
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
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