Last active
August 29, 2015 14:12
-
-
Save unscriptable/1e33a977fd8a6c4a6bae to your computer and use it in GitHub Desktop.
One solution for dealing with the parallel nature of neurons when using *networks of streams* instead of *neural networks*. (This probably doesn't help with *massively parallel* situations.)
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
var most = require('most'); | |
module.exports = filterGroup; | |
/** | |
* Filters each of a group (array) of streams via a predicate that | |
* compares each value passing through the group to the most recent | |
* value of every other stream in the group. | |
* @param {function (a, b): boolean} predicate compares the most | |
* recent value of a stream (a) to the current value passing through | |
* the stream (b). | |
* @param {Array<Stream>} streams | |
* @returns Array<Stream> | |
*/ | |
function filterGroup (predicate, streams) { | |
// Create a group stream which merges all the streams. | |
// First convert the value streams to streams of tuples: | |
// { stream, value }. | |
var groupStream = most.merge(streams.map(toStreamValueTuple)); | |
// Map each stream to a sampled version. | |
return streams.map(function (stream) { | |
var store, pred, sampler; | |
// Keep the latest value for each stream. The tuple is now: | |
// { stream, value, stored }. | |
store = storeValue(stream); | |
// Convert the predicate to work on a tuple. | |
pred = toTuplePredicate(predicate); | |
// Create the sampled stream. | |
sampler = groupStream.map(store).filter(pred); | |
return stream.sampleWith(sampler); | |
}); | |
} | |
/** | |
* Creates a stream that passes through a tuple of the original stream | |
* and the current value: { stream, value }. | |
*/ | |
function toStreamValueTuple (stream) { | |
return stream.map(tuple); | |
function tuple (value) { | |
return { stream: stream, value: value }; | |
} | |
} | |
/** | |
* Create a function that stores the most recent value of a stream | |
* and adds it to the tuple. | |
*/ | |
function storeValue (stream) { | |
var stored; | |
return function (tuple) { | |
if (tuple.stream === stream) { | |
stored = tuple.value; | |
} | |
return { | |
stream: stream, | |
value: value, | |
stored: stored | |
}; | |
} | |
} | |
/** | |
* Converts a typical value-value comparison predicate to one that | |
* can be used on the tuples used herein. | |
*/ | |
function toTuplePredicate (pred) { | |
return function (tuple) { | |
return pred(tuple.stored, tuple.value); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment