Last active
December 16, 2015 16:39
-
-
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.
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
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