Created
January 23, 2009 17:07
-
-
Save cho45/51082 to your computer and use it in GitHub Desktop.
callcc on jsdeferred
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @require http://svn.coderepos.org/share/lang/javascript/jsdeferred/trunk/jsdeferred.js | |
function callcc (fun) { | |
var error = new Deferred(); | |
return call(function () { | |
// JSDeferred は this に現在実行中の Deferred オブジェクトをいれてくる。 | |
var ccdeferred = this; | |
// 継続 (Deferred) に値を注入する関数 (Scheme の継続みたいなの) を渡してやる。 | |
return fun(function (a) { ccdeferred._next.call(a); throw error }); | |
}). | |
error(function (e) { | |
if (e === error) { | |
return e; | |
} else { | |
throw e; | |
} | |
}); | |
} | |
var cont; | |
var i = 0; | |
callcc(function (c) { | |
cont = c; | |
return 10; | |
}). | |
next(function (val) { | |
log("callcc returns:" + val); | |
if (!i++) cont(20); | |
}); | |
// should show "callcc returns:10", "callcc returns:20" | |
callcc(function (cont) { | |
return 10 * 10 * cont(20); | |
}). | |
next(function (val) { | |
log("calcc returns:" + val); | |
}); | |
// should show "callcc returns:20" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment