Skip to content

Instantly share code, notes, and snippets.

View cwharris's full-sized avatar
🖖

Christopher Harris cwharris

🖖
View GitHub Profile
@cwharris
cwharris / switch-expression-ish.js
Last active December 28, 2015 01:39
For those of you who hate living without switch-expressions.
var n = Math.floor(Math.random() * 4);
var result = n === 1 ? "one"
: n === 2 ? "two"
: n === 3 ? "three"
: "missing-no";
class Program
{
static void Main(string[] args)
{
Func<int, int, int> add = (a, b) => a + b;
var obs1 = Observable.Empty<int>().Scan(add);
var obs2 = Observable.Empty<int>().Scan(0, add);
var obs3 = Observable.Range(1, 3).Scan(add);
@cwharris
cwharris / FlowLogic.js
Last active December 23, 2015 06:59
Free drink on checkin. `pendingPours` will be incremented each time a user checks into Four Square. :)
var Rx = require('rx'),
Rx = require('./rx.helpers')
;
var FlowLogic = (function () {
Rx.Internals.inherits(FlowLogic, Rx.Observable);
// pendingPours is a behavior subject.
// solenoid is an observer. It represents the valve allowing drink flow.
@cwharris
cwharris / example.js
Created September 2, 2013 23:14
Rx.Observable.latestOn
var model = new Rx.Subject();
var click = new Rx.Subject();
model.latestOn(click, function (model, e) { return [model, e]; })
.subscribe(function (x) {
console.log(x);
});
@cwharris
cwharris / join-pattern.js
Created September 2, 2013 20:18
What should this be doing exactly?
var Rx = require('rx');
var a = new Rx.Subject();
var b = new Rx.Subject();
var c = new Rx.Subject();
var resA = Rx.Observable
.when(
a.and(b)
.then(function (a, b) { return a + b; })
@cwharris
cwharris / autoPublish-1.js
Last active December 22, 2015 02:59
autoPublish
Rx.Observable.prototype.autoPublish = function () {
var source = this.publish(),
subscribers = 0,
connection = null,
dispose = function () {
if (--subscribers === 0) { connection.dispose(); }
};
return Rx.Observable.createWithDisposable(function (o) {
if (++subscribers === 1) { connection = source.connect(); }
@cwharris
cwharris / rxdd.js
Last active December 17, 2015 04:38
Stick THIS in your browser and run it.
Rx.Observable.select = function (element, source) {
return source.select(function (value) {
return {
element: element,
value: value
};
});
};
Rx.Observable.prototype.obsFilter = function (func) {
var self = this;
return Rx.Observable.createWithDisposable(function (o) {
self.subscribe(function (source) {
var subject = null;
@cwharris
cwharris / Program.cs
Last active December 16, 2015 22:49
Reactive Primitives
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace ReactivePrimitives
{
class Program
{
static void Main(string[] args)
{
@cwharris
cwharris / flatMap.js
Created May 2, 2013 18:20
OH LOOK IT IS GOOD FRIEND FLAT MAP COME TO SAVE US ALL FROM DOOM.
function flatMap (array, func) {
return Array.prototype.concat.apply([], array.map(func));
};