Created
December 18, 2012 14:47
-
-
Save anonymous/4328616 to your computer and use it in GitHub Desktop.
Functional Reactive Programming (FRP)をjsで手書きするとこんな感じになるか。
This file contains hidden or 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('allthemagicalstuff'); | |
//event object constructor; | |
function event (target, eventName) { | |
return { | |
target: target | |
, eventName: eventName | |
}; | |
}; | |
/* | |
* SIGNALS | |
*/ | |
var MouseE , mouseS; | |
MouseE.clicks = event(document, 'click'); | |
MouseS.position = function (e) { | |
return {x:e.pageX, y:pageY}; | |
}; | |
//under the hood | |
function sampleOn (ev, signal) { | |
var samples = []; | |
var signal = extend(function () { | |
return samples; | |
}, EventEmitter); | |
ev.target.addEventListener(ev.eventName, function (e) { | |
samples.push(signal(e)); | |
signal.fire('change'); | |
}); | |
return signal; | |
} | |
var clickLocations = sampleOn(MouseE.clicks, MouseS.position); //is a compound signal | |
//capture the scene | |
function scene (locs) { | |
return map(drawPoint, locs); | |
}; | |
function drawPoint (pt) { | |
return canvas.point(pt.x, pt.y); | |
}; | |
function lift (f, sig) { | |
sig.addEventListener('change'); | |
f(sig()); | |
} | |
scene(clickLocations); | |
lift(scene, clickLocations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment