made with esnextbin
Last active
February 10, 2016 23:05
-
-
Save TylorS/61f034840047e9329247 to your computer and use it in GitHub Desktop.
esnextbin sketch
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNext Bin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
<div id="app"></div> | |
</body> | |
</html> |
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
// withLatestFrom code | |
const {zip, multicast, sampleWith} = require('most'); | |
const sampleMap = | |
sampler => stream => sampleWith(sampler, stream); | |
const withLatestFrom = | |
(fn, stream, ...streams) => | |
{ | |
const mStream = multicast(stream); | |
return zip(fn, mStream, ...streams.map(sampleMap(mStream))); | |
} | |
// Testing code | |
const {periodic, take, observe} = require('most'); | |
const add = (x, y) => x + y; | |
const add5 = (x, _) => x + 5; | |
const s1 = periodic(20, 1).scan(add, 0); | |
const s2 = periodic(100).scan(add5, 0); | |
const sampler = periodic(1000, 1).scan(add, 0); | |
const fn =(sampler, s1, s2) => ({sampler, s1, s2}); | |
const createChildElement = | |
string => | |
{ | |
const element = document.createElement('div'); | |
element.innerHTML = `<h1>${string}</h1>`; | |
document.querySelector('#app').appendChild(element); | |
} | |
const observer = | |
({sampler, s1, s2}) => | |
{ | |
createChildElement(`Sampler: ${sampler}`); | |
createChildElement(`Stream 1: ${s1}`); | |
createChildElement(`Stream 2: ${s2}`); | |
} | |
observe(observer, take(5, withLatestFrom(fn, sampler, s1, s2))); |
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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"most": "0.18.0", | |
"babel-runtime": "6.3.19" | |
} | |
} |
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
'use strict'; | |
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | |
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
// withLatestFrom code | |
var _require = require('most'); | |
var zip = _require.zip; | |
var multicast = _require.multicast; | |
var sampleWith = _require.sampleWith; | |
var sampleMap = function sampleMap(sampler) { | |
return function (stream) { | |
return sampleWith(sampler, stream); | |
}; | |
}; | |
var withLatestFrom = function withLatestFrom(fn, stream) { | |
for (var _len = arguments.length, streams = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | |
streams[_key - 2] = arguments[_key]; | |
} | |
var mStream = multicast(stream); | |
return zip.apply(undefined, [fn, mStream].concat((0, _toConsumableArray3.default)(streams.map(sampleMap(mStream))))); | |
}; | |
// Testing code | |
var _require2 = require('most'); | |
var periodic = _require2.periodic; | |
var take = _require2.take; | |
var observe = _require2.observe; | |
var add = function add(x, y) { | |
return x + y; | |
}; | |
var add5 = function add5(x, _) { | |
return x + 5; | |
}; | |
var s1 = periodic(20, 1).scan(add, 0); | |
var s2 = periodic(100).scan(add5, 0); | |
var sampler = periodic(1000, 1).scan(add, 0); | |
var fn = function fn(sampler, s1, s2) { | |
return { sampler: sampler, s1: s1, s2: s2 }; | |
}; | |
var createChildElement = function createChildElement(string) { | |
var element = document.createElement('div'); | |
element.innerHTML = '<h1>' + string + '</h1>'; | |
document.querySelector('#app').appendChild(element); | |
}; | |
var observer = function observer(_ref) { | |
var sampler = _ref.sampler; | |
var s1 = _ref.s1; | |
var s2 = _ref.s2; | |
createChildElement('Sampler: ' + sampler); | |
createChildElement('Stream 1: ' + s1); | |
createChildElement('Stream 2: ' + s2); | |
}; | |
observe(observer, take(5, withLatestFrom(fn, sampler, s1, s2))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment