Skip to content

Instantly share code, notes, and snippets.

View gist:29af75aeca49b88b7ce866c83eb3c481
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;
@mocheng
mocheng / gist:ac1304e61380de22045105520e6007d6
Created Jun 13, 2017
Implement Redux Store with Rx.js
View gist:ac1304e61380de22045105520e6007d6
'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 => {
View gist:2b8e30ef03fb69f26f9a038e6199b71c
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));
};
View gist:39a10055509fd3e7b193f72b03b831dd
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));
@mocheng
mocheng / applicative
Created Sep 23, 2016
Applicative in JavaScript
View applicative
Array.prototype.ap = function(wrappedVals) {
var results = [];
this.map( (f)=> {
const mr = wrappedVals.map(f);
results = results.concat(mr);
});
return results;
}
@mocheng
mocheng / dev.conf
Created Feb 16, 2012
Setup of dev/test/prod environment
View dev.conf
<VirtualHost *>
ServerName xxx.xxx.com
DocumentRoot "/var/www/html"
DirectoryIndex index.php
SetEnv XXX_ENV "development"
</VirtualHost>
@mocheng
mocheng / .htaccess
Created Feb 13, 2012
.htaccess for CodeIgniter
View .htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|style|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
@mocheng
mocheng / Change in macrolib.xml
Created Jul 22, 2011
Customize YUIBuilder to integrate mustache
View Change in macrolib.xml
<copy file="${builddir}/files/moduletemplate.txt" tofile="@{file}" overwrite="true">
<filterset>
<filter token="CODE" value="${@{module}-@{file}-code}" />
<filter token="YUIVAR" value="${yui.variable}" />
<filter token="MODULE" value="@{module}" />
<filter token="DETAILS" value="${@{module}-details}" />
<filter token="VERSION" value="${component.version}" />
<filter token="MUSTACHE_TEMPLATE" value="${component.mustache.template}" />
</filterset>
</copy>
@mocheng
mocheng / override
Created Jun 22, 2011
override object method and recover it.
View override
var foo = obj.foo;
obj.foo = function() {
// do something
obj.foo = foo;
obj.foo(arguments);
};
@mocheng
mocheng / index.html
Created Jun 9, 2011
Simple MVC demo in Node.js
View index.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>MVC Demo</title>
</head>
<body>
{{name}} is {{title}}.
</body>
</html>