Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Last active December 10, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympbyc/4357622 to your computer and use it in GitHub Desktop.
Save ympbyc/4357622 to your computer and use it in GitHub Desktop.
var A = ActorBeaver();
A.define('fact', function (n, cont) {
if (n === 0) cont(1);
else A.message('fact', n - 1, function (f) { A.message('multiply', n, f, cont); });
});
A.define('multiply', function (x, y, cont) {
//defineされていないから普通の関数呼び出しに見えるけどこれもmessage
cont(x * y);
});
A.message('fact', 5, alert);
/* ----- Actor Beaver ----- */
function ActorBeaver () {
actors = {};
var API = {};
API.define = function (address, actor) {
if (!!actors[address]) throw new Error('Multiple definition');
actors[address] = actor;
};
API.message = function (address /* . arguments[1..] */) {
var arg = arguments;
setTimeout(function () {
actors[address].apply({}, [].slice.call(arg, 1));
}, 0);
};
return API;
}
//alphaがactorを作る - ここではfunction = alpha
//actorは次にメッセージを送るactorが決まっているものと決まっていないものがある
//決まっていない物には継続actorを送ってやる
//returnはない
//ifはある
//eg
//[+ 1 2
// (alpha (u) ; c
// [* 5 u display])]
//+ や * はプリミティブアクター
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment