Skip to content

Instantly share code, notes, and snippets.

@kragen
Created August 22, 2012 20:11
Show Gist options
  • Save kragen/3428922 to your computer and use it in GitHub Desktop.
Save kragen/3428922 to your computer and use it in GitHub Desktop.
// Original code in Scheme, from
// <http://www.cap-lore.com/CapTheory/Language/lambdaSecurity.html>:
//
// (define (mocntr) (let ((x 0)) (cons (lambda () (let ((yet #t))
// (lambda () (if yet (begin (set! yet #f) (set! x (+ 1 x)) x)))))
// (lambda () x))))
// (define restaurants (list "Mandarin" "Casa Lupe" "Coffee Shop"))
// (define (noontime) (let ((bb
// (map (lambda(r) (cons r (mocntr))) restaurants)))
// (for-each (lambda (e) ((cdr e) (cons "Lunchtime!" (map
// (lambda(r)(cons (car r) ((cadr r)))) bb)))) lunchAgents)
// (lambda ()(map (lambda(x) (cons (car x) ((cddr x)))) bb))))
// (define lunchcrowd (list 'Jim 'Sue 'Tom 'Jennifer))
//
// (define lunchAgents (map (lambda (eater) (let ((cell
// (cons eater (lambda (ballot) '()))))
// ('send eater "LunchAgentAppointer"
// (lambda (agent) (set-cdr! cell agent))) cell)) lunchcrowd))
function mocntr() {
var x = 0;
return { voterMaker: function() {
var yet = true;
return function() {
if (!yet) return;
yet = false;
x++;
return x;
};
}
, votes: function() {
return x;
}
};
}
var restaurants = ["Mandarin", "Casa Lupe", "Coffee Shop"];
var lunchCrowd = ['Jim', 'Sue', 'Tom', 'Jennifer'];
var lunchAgents = [];
for (var ii = 0; ii < lunchCrowd.length; ii++) {
var eater = lunchCrowd[ii];
lunchAgents.push((function() {
var cell = { eater: eater, ballotReceiver: function(ballot) {} };
SEND(eater, "LunchAgentAppointer",
function(agent) { cell.ballotReceiver = agent });
return cell;
})());
}
function noontime() {
var bb = [];
for (var ii = 0; ii < restaurants.length; ii++) {
(function() {
var restaurant = restaurants[ii];
bb.push({restaurant: restaurant, counter: mocntr()});
})()
}
for (ii = 0; ii < lunchAgents.length; ii++) {
var e = lunchAgents[ii]
, ballot = []
;
for (var jj = 0; jj < bb.length; jj++) {
ballot.push({ restaurant: bb[jj].restaurant
, vote: bb[jj].voterMaker()
});
}
e.send(["Lunchtime!", ballot]);
}
return function() {
var votes = [];
for (var ii = 0; ii < bb.length; ii++) {
votes.push({ restaurant: bb[ii].restaurant
, votes: bb[ii].counter.votes()
});
}
return votes;
};
}
@kragen
Copy link
Author

kragen commented Aug 22, 2012

Still fixing some bugs... and rewriting in CoffeeScript!

@kragen
Copy link
Author

kragen commented Aug 22, 2012

Okay. There are still bugs in the JS version. The CS version is at https://gist.github.com/3429195.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment