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
// Globals | |
declare const foo: Function; | |
declare const bar: Function; | |
declare const dict: Object; | |
// Option 1 (bad) | |
const functions1 = [ | |
{fn: foo, key: 'FOO'}, | |
{fn: bar, key: 'BAR'} | |
]; |
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
// Problem | |
function handler(event: Event) { | |
let element = event as HTMLElement; // Error : Neither 'Event' not type 'HTMLElement' is assignable to the other | |
} | |
// Approach 1 (using any) | |
function handler(event: Event) { | |
let element = event as any as HTMLElement; // Basarat calls this "double assertion" | |
} |
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 called = (function() { | |
var count = 0; | |
return function() { | |
count++; | |
console.log(`Called : ${count}`); | |
}; | |
}()); | |
called(); //=> Called : 1 | |
called(); //=> Called : 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
// Callback-style (recursion explicit) | |
function forAsync(items, fn, next) { | |
var max = items.length; | |
function rec(i) { | |
if (i === max) { | |
next(); | |
} | |
fn(items[i], i); | |
rec(i + 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
// Scheduler is setTimeout with an ability to delay | |
// already scheduled tasks. | |
// 1. DEFINITION | |
// ------------- | |
function Scheduler(freq) { // freq in ms | |
this.freq = freq; | |
setInterval(this._main.bind(this), this.freq); | |
} |
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
// In a language with arrays, `reduce` can be used | |
// to accumulate arrays and thus can be used to | |
// define array-producing operators like `map`. | |
Array.prototype.map = function(fn) { | |
return this.reduce(function(sum, cur) { | |
return sum.concat(fn(cur)); | |
}, []); | |
}; |
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
# 0. MOTIVATION | |
# If you're working with two git branches, it might be convenient | |
# to have a way of quickly toggling between them. | |
# Update: Eric D. Wang has informed me of the built-in | |
# `git checkout -` command (analogous to the usual `cd -`) | |
# that provides the same functionality. Please use that | |
# instead. | |
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
/* A fixed point combinator */ | |
function fix(f) { | |
var g = function(x){return f(function(y){return x(x)(y);});}; | |
return g(g); | |
} | |
/* An indirectly recursive factorial function. */ | |
function factorial(self) { | |
return function(n) { | |
if (n == 0) {return 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
""" | |
Intended to be used as a decorator, UniformGuard | |
ensures that all of the arguments passed to its | |
decorated function `_f` satisfy the guard `g`. | |
""" | |
def UniformGuard(g, e=TypeError): | |
def _g(f): | |
def _f(*args, **kwargs): | |
if not all(map(g, set(args).union(kwargs.values()))): | |
if e: |