CSE 130 Lecture Notes - Lecture 15: If And Only If, Nested Function, Firstclass

39 views5 pages
2 Mar 2018
How continuations do async programming!
fs.readFile( blah, callback );
Thread0 : | r | | a | | |
Thread1 : | c | | |
-allows for a form of concurrency
-”continue” doing something else
When you write an explicit callback
- Cooperative multi-threading (in contrast to preemptive multi-threading - stops
processes whether you want to or not)
- We yield() - explicitly tell OS or Runtime the state the save and how to restore
Ex: debugger
Breakpoints: stop execution at a certain point
- To examine the state of the program (memory)
- Want to be able to keep going - can continue running the program (have continuation to
rest of program!)
Continuations are implicit in your code
Consider ( 2*x + 1/y ) * 2
A. multiply 2 and x //previous
B. Divide 1 by y //current
C. Add A and B
D. Multiply C and 2
let previous = 2 * x
Let cont = curRes => (previous + curRes) * 2
cont( 1 / y ) current computation
Node.js example
const data = fs.readFileSync(‘myFile.txt’)
processData(data); ← continuation
Explicit continuation (what you have to do today!)
fs.readFile(‘myFile.txt’, callback)
Function callback (err, data) {
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Continuation Passing Style (CPS)
-call/cc (call w/ current continuation) is used to call a function and give it the current
-why is this powerful?
-we want to bail out! We want to know WHERE to continue if we got an error!
-MOST languages don’t give you this!
-so we have to transform our code to CPS
W/ call/cc:
K <- get Current Continuation
fs.read( function () {
Function () {
… K()
} //need to bail out of here!
-why do we want to do this?
- makes control flow explicit: NO return!
- order of evaluation - have to specify the order of DOING things
-Why do we care?
- Optimizations:
- we don’t need stack frames! We can just jump to wherever we need to continue!
- threading - iff our runtime allows it!
- Intermediate Representations
To CPS, by example
Function zero() { function zero ( cc ) { //cc is our program continuation!
Return 0; cc(0);
} }
Function fact(n) { function fact( n, cc) {
If (n == 0) { if (n == 0) {
Return 1; cc(1);
} else { } else {
Return n* fact(n-1); fact( n-1, r => cc(r*n))
} }
} }
Note that the return n*fact(n-1) does NOT return immediately
fact(3): CPS fact(3, x => x)
fact(3, id) ->
fact(2, rA => id(3 * rA)) ->
fact(1, rb => (rA => id(3 * rA))(2 * rb)) ->
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.