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
Continuations in Ruby
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Antono Vasiljev
June 17, 2012
140
0
Share
Continuations in Ruby
Antono Vasiljev
June 17, 2012
More Decks by Antono Vasiljev
See All by Antono Vasiljev
Node.js Overview
antono
1
330
Cucumber
antono
1
330
Featured
See All Featured
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
460
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
240
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
200
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
310
Joys of Absence: A Defence of Solitary Play
codingconduct
1
380
Un-Boring Meetings
codingconduct
0
300
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
470
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
22k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Into the Great Unknown - MozCon
thekraken
41
2.5k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
120
What's in a price? How to price your products and services
michaelherold
247
13k
Transcript
Continuations in Ruby Antono Vasiljev Ruby Open Air, 2012
Continuation is basically Rest of the program
Continuations are first class objects in Ruby
irb(main)> c = callcc { |cont| cont } => #<Continuation:0x7f70ad16d030>
Continuations are about Flow Control
1: i = 0 2: callcc do |cont| 3: i
= 1 4: cont.call 5: i = 2 # skipped 6: end 7: 8: # i => 1
1: i = 0 2: i = callcc do |cont|
3: i = 1 4: cont.call(2) 5: i = 3 # skipped 6: end 7: 8: # i => 2 # See lines: 2, 4
1| i = 0 2| 3| label :sum 4| 5|
puts i += 1 6| 7| goto :sum, cond: (i < 10)
1| LABELS = {} 2| 3| def label(name) 4| callcc
{ |cont| LABELS[name] = cont } 5| end 6| 7| def goto(label, args: {}) 8| LABELS[label].call if args[:cond] 9| end
Continuation are like goto with parameters (but jumps only backward)
continuations jumps far def foo bar end def bar baz
end def baz $cont[0] end callcc { |c| $cont = c; foo }
Similar to exceptions. But can go down through stack. Time
Machine!
Restartable Exceptions
1| begin 2| hello 3| rescue Exception => e 4|
e.restart 5| ensure 6| e.cleanup 7| end
1| def hello 3| i = 0 4| restartable do
5| puts i += 1 6| raise Exception unless i == 5 7| end 8| end
1| def restartable 2| cont = callcc { |c| c
} 3| begin 4| yield 5| rescue Exception => e 6| e.continuation = cont 7| raise e 8| end 9| end
require 'continuation' class Exception class << self attr_accessor :conts end
def continuation=(cont) self.class.conts ||= {} self.class.conts[self.class] ||= cont end
def restart self.class.conts[self.class].call end def cleanup self.class.conts.delete(self.class) end end
You can do with continuation: Generator objects Fibers/Coroutines Exit from
recursion Other control structures
Thanks!
http://antono.info/