Skip to content

Instantly share code, notes, and snippets.

@mocheng
mocheng / storm with publish
Last active August 29, 2015 14:04
storm mosca server
var mqtt = require('mqtt');
var port = 1883;
var host = 'localhost';
var options = {
username: 'my_username',
password: 'my_password',
clientId: 'my_client_id'
};
var client = mqtt.createClient(port, host, options);
@mocheng
mocheng / gist:aa87a28adfb00220f169
Created September 18, 2014 03:01
build git 1.8.1.2
mkdir -p ~/Downloads
cd ~/Downloads
sudo yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel curl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
wget -O v1.8.1.2.tar.gz https://github.com/git/git/archive/v1.8.1.2.tar.gz
tar -xzvf ./v1.8.1.2.tar.gz
cd git-1.8.1.2/
@mocheng
mocheng / 0_reuse_code.js
Last active August 29, 2015 14:11
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@mocheng
mocheng / applicative
Created September 23, 2016 06:43
Applicative in JavaScript
Array.prototype.ap = function(wrappedVals) {
var results = [];
this.map( (f)=> {
const mr = wrappedVals.map(f);
results = results.concat(mr);
});
return results;
}
@mocheng
mocheng / gist:29af75aeca49b88b7ce866c83eb3c481
Created September 23, 2016 06:52
flatMap/monad in JavaScript
Array.prototype.flatMap = function(lambda) {
return [].concat.apply([], this.map(lambda));
};
const r = [1, 2, 3].flatMap(x => {
var arr = new Array(x);
for (var i=0; i<arr.length; ++i) {
arr[i] = x;
}
return arr;
const Rx = require('../../');
const fs = require('fs');
const t = require('transducers-js');
const source = Rx.Observable.range(1, 10000);
const increment = x => x + 1;
const isEven = x => (x % 2 === 0);
const startTime = new Date();
const transducer = t.comp(t.map(increment), t.filter(isEven));
const Rx = require('../../');
const fs = require('fs');
const t = require('transducers-js');
const R = require('ramda');
const _ = require('lodash');
const mappingReducer = f => reducer => (result, input) => {
//console.log('enter mappingReducer', result, input);
return reducer(result, f(input));
};
@mocheng
mocheng / gist:ac1304e61380de22045105520e6007d6
Created June 13, 2017 08:19
Implement Redux Store with Rx.js
'use strict';
const Rx = require('rxjs');
const createStore = (reducer, initialState = {}) => {
const action$ = new Rx.Subject();
let currentState = initialState;
const store$ = action$.startWith(initialState).scan(reducer).do(
state => {