Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / race.md
Last active March 4, 2016 17:12
Recipe for racing two streams

Needs a description, and a meaningful example ...

import { merge } from 'most'

// race :: Stream a -> Stream a -> Stream a
// return a stream that imitates the input stream with
// the earliest first event
const race = (s1, s2) =>
 merge(mapToSelf(s1), mapToSelf(s2)).take(1).join()
@briancavalier
briancavalier / index.js
Created February 10, 2016 13:39
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var most = require('most');
alert(most);
import { periodic, sample, filter, multicast } from '../most';
// stream of values we want to filter
// values :: Stream String
const values = periodic(100, 'hi');
// filter condition. For fun, this one just periodically flip-flops
// between true and false, but could be any stream of booleans
// allow :: Stream boolean
const allow = periodic(1000).scan(x => !x, false);
@briancavalier
briancavalier / most.js
Last active January 10, 2017 19:37
Beginnings of a flow type declaration file for most.js
/*@flow*/
export type SeedValue<S, V> = { seed: S, value: V };
export type TimeValue<V> = { time: number, value: V };
type CreateGenerator<A> = (...args:Array<any>) => Generator<A|Promise<A>, any, any>
export type Stream<A> = {
reduce<B>(f:(b:B, a:A) => B, b:B): Promise<B>;
observe(f:(a:A) => any): Promise<any>;
'use strict';
import { runNode, all, coroutine } from 'creed';
import { readFile } from 'fs';
import { join } from 'path';
// joinPath :: String -> String -> String
const joinPath = init => tail => join(init, tail);
// readFileP :: String -> String -> Promise Error Buffer
'use strict';
import { runNode, all, coroutine } from 'creed';
import { readFile } from 'fs';
import { join } from 'path';
const joinPath = init => tail => join(init, tail);
const readFileP = encoding => file => runNode(readFile, file, {encoding});
const compose = (f, g) => x => g(f(x));
@briancavalier
briancavalier / index.js
Created May 20, 2015 16:27
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var Promise = require('when').Promise;
var promise = Promise.resolve({});
promise.then(function (value) {
@briancavalier
briancavalier / observables-cujojs-most.js
Created May 15, 2015 14:23
most.js results for Bluebird doxbee perf test
global.useMost = true;
var most = require('most');
require('../lib/fakesObservable');
module.exports = function upload(stream, idOrPath, tag, done) {
var blob = blobManager.create(account);
var tx = db.begin();
var blobIdP = blob.put(stream);
var fileP = self.byUuidOrPath(idOrPath).get();
var version, fileId, file;
var when = require('when');
var wire = require('../../wire');
require('when/monitor/console');
var logger = console;
var contextDefinition = {
$plugins: [
//{ module: '../../debug' },
{ module: '../../aop' }
let i=0;
let xs = Observable.fromEvent('click', aDomNode);
// or
// let xs = Observable.fromArray([1,2,3,4]);
for(let x on xs) {
++i;
}
// What does this log?
console.log(i);