This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imperative | |
function foo(bar) { | |
if (!bar) { | |
return ; | |
} | |
return bar + 1; | |
} | |
foo(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } } | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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: | |
*/ |