made with esnextbin
Last active
June 3, 2017 18:44
-
-
Save TylorS/d44d5c7145cd28f5613a665481699138 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>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</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
import { runEffects, tap, fromPromise, periodic, constant, scan } from '@most/core' | |
import { newDefaultScheduler } from '@most/scheduler' | |
class Just { | |
constructor (value) { | |
this.value = value | |
} | |
next() { | |
return { value: this.value, done: true } | |
} | |
} | |
class Map { | |
constructor (f, iterable) { | |
this.f = f | |
this.iterable = iterable | |
} | |
next() { | |
const { f, iterable } = this | |
const { value, done } = iterable.next() | |
return { value: f(value), done } | |
} | |
} | |
class Sample { | |
constructor (f, stream, behavior) { | |
this.f = f | |
this.stream = stream | |
this.behavior = behavior | |
} | |
run(sink, scheduler) { | |
const { f, stream, behavior } = this | |
return stream.run(new SampleSink(f, behavior, sink), scheduler) | |
} | |
} | |
class SampleSink { | |
constructor (f, behavior, sink) { | |
this.f = f | |
this.behavior = behavior | |
this.sink = sink | |
} | |
event(t, x) { | |
const { f, behavior, sink } = this | |
const { value } = behavior.next() | |
sink.event(t, f(value, x)) | |
} | |
error (t, e) { | |
this.sink.error(t, e) | |
} | |
end (t) { | |
this.sink.end(t) | |
} | |
} | |
// Example | |
const scheduler = newDefaultScheduler() | |
const drain = stream => runEffects(stream, scheduler) | |
const observe = (f, stream) => drain(tap(f, stream)) | |
const just = x => new Just(x) | |
const map = (f, iterable) => new Map(f, iterable) | |
const sample = (f, behavior, stream) => new Sample(f, stream, behavior) | |
const behavior = map((x) => x + 1, just(100)) | |
const add = (x, y) => x + y | |
const sampler= scan(add, 0, constant(1, periodic(100))) | |
const stream = sample(add, behavior, sampler) | |
observe(console.log, stream) |
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/core": "0.3.1", | |
"@most/scheduler": "0.4.0", | |
"babel-runtime": "6.22.0" | |
} | |
} |
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 _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
var _createClass2 = require('babel-runtime/helpers/createClass'); | |
var _createClass3 = _interopRequireDefault(_createClass2); | |
var _core = require('@most/core'); | |
var _scheduler = require('@most/scheduler'); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var Just = function () { | |
function Just(value) { | |
(0, _classCallCheck3.default)(this, Just); | |
this.value = value; | |
} | |
(0, _createClass3.default)(Just, [{ | |
key: 'next', | |
value: function next() { | |
return { value: this.value, done: true }; | |
} | |
}]); | |
return Just; | |
}(); | |
var Map = function () { | |
function Map(f, iterable) { | |
(0, _classCallCheck3.default)(this, Map); | |
this.f = f; | |
this.iterable = iterable; | |
} | |
(0, _createClass3.default)(Map, [{ | |
key: 'next', | |
value: function next() { | |
var f = this.f; | |
var iterable = this.iterable; | |
var _iterable$next = iterable.next(); | |
var value = _iterable$next.value; | |
var done = _iterable$next.done; | |
return { value: f(value), done: done }; | |
} | |
}]); | |
return Map; | |
}(); | |
var Sample = function () { | |
function Sample(f, stream, behavior) { | |
(0, _classCallCheck3.default)(this, Sample); | |
this.f = f; | |
this.stream = stream; | |
this.behavior = behavior; | |
} | |
(0, _createClass3.default)(Sample, [{ | |
key: 'run', | |
value: function run(sink, scheduler) { | |
var f = this.f; | |
var stream = this.stream; | |
var behavior = this.behavior; | |
return stream.run(new SampleSink(f, behavior, sink), scheduler); | |
} | |
}]); | |
return Sample; | |
}(); | |
var SampleSink = function () { | |
function SampleSink(f, behavior, sink) { | |
(0, _classCallCheck3.default)(this, SampleSink); | |
this.f = f; | |
this.behavior = behavior; | |
this.sink = sink; | |
} | |
(0, _createClass3.default)(SampleSink, [{ | |
key: 'event', | |
value: function event(t, x) { | |
var f = this.f; | |
var behavior = this.behavior; | |
var sink = this.sink; | |
var _behavior$next = behavior.next(); | |
var value = _behavior$next.value; | |
sink.event(t, f(value, x)); | |
} | |
}, { | |
key: 'error', | |
value: function error(t, e) { | |
this.sink.error(t, e); | |
} | |
}, { | |
key: 'end', | |
value: function end(t) { | |
this.sink.end(t); | |
} | |
}]); | |
return SampleSink; | |
}(); | |
// Example | |
var scheduler = (0, _scheduler.newDefaultScheduler)(); | |
var drain = function drain(stream) { | |
return (0, _core.runEffects)(stream, scheduler); | |
}; | |
var observe = function observe(f, stream) { | |
return drain((0, _core.tap)(f, stream)); | |
}; | |
var just = function just(x) { | |
return new Just(x); | |
}; | |
var map = function map(f, iterable) { | |
return new Map(f, iterable); | |
}; | |
var sample = function sample(f, behavior, stream) { | |
return new Sample(f, stream, behavior); | |
}; | |
var behavior = map(function (x) { | |
return x + 1; | |
}, just(100)); | |
var add = function add(x, y) { | |
return x + y; | |
}; | |
var sampler = (0, _core.scan)(add, 0, (0, _core.constant)(1, (0, _core.periodic)(100))); | |
var stream = sample(add, behavior, sampler); | |
observe(console.log, stream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment