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
def memo(f): | |
"""Basic memoizer for positional parameters.""" | |
table = {} | |
def _f(*args): | |
if (args) not in table: | |
table[(args)] = f(*args) | |
return table[(args)] | |
return _f |
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
from collections import Counter | |
def sort(lst): | |
return [i for i, n in Counter(lst).items() for _ in range(n)] |
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
/* | |
Calling Counter with (a possibly `undefined`) `init` | |
returns a function that closes over `counter` and | |
increments it every time it's called. | |
*/ | |
function Counter(init) { | |
var counter = init || 1; | |
return function() { | |
var current = counter; | |
counter += 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
// What does scan*(id, op, arr) do? | |
// | |
// Given an identity element `id`, a binary associative | |
// operator `op`, and a list `arr`, returns a list of the | |
// same size where each item in that list is the op-sum | |
// of all previous items. | |
// | |
// E.g. sum(0, +, [3, 4]) => [(0 + 3), (3 + 4)] | |
// E.g. sum(1, *, [2, 5]) => [(1 * 2), (2 * 5)] |
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
// 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. | |