Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
zhangchiqing / leftPad.js
Created March 30, 2016 01:16
leftPad.js
'use strict';
var R = require('ramda');
// leftPad :: Number -> String -> String
// > leftPad(3, '0', '7')
// '007'
// > leftPad(3, '0', '2016')
// '2016'
var leftPad = R.curry(function(num, fill, str) {
// Imperative
function foo(bar) {
if (!bar) {
return ;
}
return bar + 1;
}
foo(null);
@zhangchiqing
zhangchiqing / .vimrc
Created March 28, 2016 00:56
vim shortcut to bind <leader>d to run doctest for typescript file
nnoremap <leader>d :call <SID>RunDoctest()<cr>
function! s:RunDoctest()
if &filetype == "typescript"
cexpr system('tsc ' . expand('%:p') . '&& doctest --module commonjs ' . substitute(expand('%:p'), ".ts", ".js", ""))
endif
if &filetype == "javascript"
cexpr system('doctest --module commonjs ' . expand('%:p'))
endif
copen
@zhangchiqing
zhangchiqing / lazy.js
Created March 17, 2016 19:43
Lazy Lambda
'use strict';
var R = require('ramda');
var expensiveMap = R.compose(R.join(''), R.repeat(R.__, 10000));
var data = R.range(1, 10000);
// $ time node lazy.js
// node lazy.js 3.42s user 0.16s system 99% cpu 3.584 total
R.take(1, R.map(expensiveMap, data));
@zhangchiqing
zhangchiqing / liftP.js
Created February 18, 2016 19:00
lift Promise
var R = require('ramda');
var Promise = require('bluebird');
var liftP2 = R.curry(function(fn, p1, p2) {
return Promise.all([p1, p2]).then(R.apply(fn));
});
R.lift(R.add)([1], [2])
// => [3]
@zhangchiqing
zhangchiqing / mongdb_distinct_count.js
Last active February 18, 2016 18:28
mongdb_distinct_count.js
db.COLLECTION_NAME.aggregate([
{ $match: { created_at: { $gte: ISODate("2016-02-05T00:00:00.000Z"), $lt: ISODate("2016-02-06T00:00:00.000Z") } } },
{ $group: { _id: '$FIELD_NAME', count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
@zhangchiqing
zhangchiqing / flatMap.js
Last active July 16, 2016 22:15
flatMap.js
/*
* flatMap can be implemented in terms of flatten and map
* which can be implemented using reduce
*/
'use strict';
// > reduce(function(sum, value) { return sum + value; },
// . 0, [1,2,3])
// 6
var reduce = function(iter, initial, arr) {
@zhangchiqing
zhangchiqing / doctest.sh
Last active January 8, 2016 05:18
doctest
# npm install --save-dev doctest
# Add "/* doctest */" to the file that contains doctest test cases
# Then use `dt` to run all those test cases
alias dt="git grep '\/\* doctest \*\/' | cut -d ':' -f 1 | xargs ./node_modules/.bin/doctest --module commonjs"
@zhangchiqing
zhangchiqing / local_module
Created January 5, 2016 19:07
Shortcut to run local node module
# Put the following code into your .bash_profile or .zshrc
local_node_module() {
./node_modules/.bin/"$*"
}
alias nn="local_node_module"
# `nn gulp` is now equivalent to `./node_module/.bin/gulp`
@zhangchiqing
zhangchiqing / list_comprehension.js
Created January 3, 2016 22:10
Implement list comprehension
/*
* Example:
* Find any integer x, y, z that match the following conditions
* 1. 0 < x < y < z < 10
* 2. x * x + y * y = z * z
* In Python:
* [(x,y,z) for x in range(1,10) for y in range(1,10) for z in range(1,10) if x < y and y < z and x * x + y * y == z * z]
* >>> [(3, 4, 5)]
* In Javascript with Ramda:
*/