Skip to content

Instantly share code, notes, and snippets.

@johnpmayer
Last active December 16, 2015 16:39
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 johnpmayer/5464492 to your computer and use it in GitHub Desktop.
Save johnpmayer/5464492 to your computer and use it in GitHub Desktop.
A shot at an unsafe loop construct Native Elm library. Not necessarily intended to be part of the standard library.
Mayer.John.Native.Loop = function(elm) {
// Various boilerplate stuff
'use strict';
if (elm.Native.Loop) return elm.Native.Loop;
var Signal = Elm.Signal(elm);
// This is the unsafe loop construct
// loop : (b -> a) -> (Signal a -> Signal b) -> b -> Signal b
var loop = function(convert, transform, initial) {
// Create the loopback Signal with the initial value
var loopback = Signal.constant(initial);
// Lift the convert pure function, and apply to the loopback
var converted = A2(Signal.lift, convert, loopback);
// Transform the converted signal using the Signal node
// This is the eventual
var output = transform(converted);
// KEY STEP
// Whenever this output signal changes, send a message to the dispatcher
// that the loopback should update in the next "round".
output.recv(function(val) {
elm.notify(loopback.id, value);
})
// Give the output signal back to the user
return output;
};
return Mayer.John.Native.Loop = { loop: F3(loop) };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment