This file contains hidden or 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 Q = require('q'), | |
LazyPromise = require('lazy-promise'); | |
function async(name) { | |
console.log('start async: ' + name); | |
var deferred = Q.defer(); | |
// Resolve the promise in the next turn of the event loop | |
process.nextTick(function () { | |
console.log('finish async: ' + name); |
This file contains hidden or 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
; compute the Ackermann's function | |
(defn A [x y] | |
(cond (= y 0) 0 | |
(= x 0) (*' 2 y) | |
(= y 1) 2 | |
:else (A (- x 1) | |
(A x (- y 1))))) | |
; f(x) = 2x | |
(defn f [n] |
This file contains hidden or 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
function combinations(arr1, arr2) { | |
return arr1.reduce(function (prev, curr) { | |
return prev.concat(arr2.map(function (item) { | |
return [curr, item]; | |
})); | |
}, []); | |
} | |
> combinations([1,2,3], ['a', 'b', 'c', 'd']); | |
[ [ 1, 'a' ], |
This file contains hidden or 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
function canHazInt(s) { | |
var n = parseInt(s, 10); | |
if (!isNaN(n)) { | |
return n; | |
} else { | |
throw new Error('no can do'); | |
} | |
} |
This file contains hidden or 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
// range0(n) -> [0, ... , (n - 1)] | |
function range0(n) { | |
return Array.apply(null, Array(n)).map(function (val, i) { | |
return i; | |
}); | |
} | |
// range(end) -> [0, ... , (end - 1)] | |
// or | |
// range(start, end) -> [start, ... , (end - 1)] |
This file contains hidden or 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 data = [ | |
{ name: 'a', other: 'ignore me', sub: [{name: 'a1'}, {name: 'a2'}] }, | |
{ name: 'b', sub: [{name: 'b1'}, {name: 'b2', another: 'ignore me too'}] } | |
]; | |
// Wanted: ['a', 'a1', 'a2', 'b', 'b1', 'b2'] | |
// Flattenezify! | |
_.chain(data).map(function (d) { |
This file contains hidden or 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
(function () { | |
'use strict'; | |
var func1 = function () {}, | |
func2 = function (callback) { callback(); }; | |
// JSLint does not complain about the lines below | |
func2(func1. | |
invalid); |
This file contains hidden or 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
// Set the drag being busy for 500ms | |
that.dragBusy = true; | |
window.setTimeout(function () { | |
that.dragBusy = false; | |
}, 100); |
This file contains hidden or 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
// In the events object of the view: | |
// 'touchstart nav a': 'onNavTouchStart', | |
// 'touchmove nav a': 'onNavTouchMove', | |
// 'touchend nav a': 'onNavTouchEnd', | |
onNavTouchStart: function (event) { | |
var href = $(event.target).attr('href'); | |
this.navTouchHref = (href && href !== '#') ? href : null; | |
}, |
This file contains hidden or 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
// one liner | |
_.tap({}, function (o) { _.each(location.hash.split('&'), function (p) { var s = p.split('='); o[s[0]] = s[1]; }); }); | |
// indented | |
_.tap({}, function (o) { | |
_.each(location.hash.split('&'), function (p) { | |
var s = p.split('='); | |
o[s[0]] = s[1]; |