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
/* | |
* Provide the implementations for 3 `add` functions to output the same value as commented | |
* without using if-else or switch-case | |
*/ | |
var num = 1; | |
var add = function(n) { | |
// to implement | |
}; | |
var addNum = add(num); | |
addNum(2); |
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'); | |
R.compose( | |
R.apply(R.add), | |
R.ap([R.add(3), R.multiply(100)]) | |
)([5]); // 508 | |
// applyMerge2 :: (a -> b) -> (a -> b) -> (b -> c) -> ([a] -> c) | |
var applyMerge2 = function(f1, f2, fmerge) { |
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: | |
*/ |
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
# 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
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
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'); | |
// 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
'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)); |