Last active
October 6, 2016 01:58
-
-
Save freddi301/389bac65a6ce3996886526d566360adb to your computer and use it in GitHub Desktop.
rx + transducers
This file contains hidden or 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
const map = f => r => (m, i) => r(m, f(i)); | |
const filter = p => r => (m, i) => (p(i) ? r(m, i) : m); | |
function conc(m, i) { return m.concat(i); } | |
[1, 2, 3].reduce( | |
map(x => (console.log('map ' + x), x + x))( | |
filter(x => (console.log('filter ' + x), x !== 4))(conc)), | |
[]); | |
class S { | |
constructor(subs, evs) { this.subs = subs || []; this.evs = evs || [] } | |
sub(su, m) { return new S(this.subs.concat({ su, m: this.evs.reduce(su, m) })); } | |
pub(e) { return new S(this.subs.map(r => ({ su: r.su, m: r.su(r.m, e) })), this.evs.concat(e)); } | |
} | |
const s = (new S).sub((m, i) => m.concat(i + 1), []).sub((m, i) => m.concat(i * 2), []).pub(2).pub(5); | |
const ss = { s, pub(e) { return this.s = this.s.pub(e); }, sub(s, m) { return this.s = this.s.sub(s, m); } }; | |
ss.sub( | |
map(x => (console.log('map ' + x), x + x))( | |
filter(x => (console.log('filter ' + x), x !== 4))(conc)) | |
, []); | |
ss.pub(1); ss.pub(2); ss.pub(3); ss.s.subs[2].m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment