Skip to content

Instantly share code, notes, and snippets.

View hunan-rostomyan's full-sized avatar

Hunan Rostomyan hunan-rostomyan

View GitHub Profile
@hunan-rostomyan
hunan-rostomyan / FixedFactorial.js
Created March 6, 2016 09:02
Defining Recursive Functions using a Fixed Point Combinator
/* 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;}
@hunan-rostomyan
hunan-rostomyan / gitback.sh
Last active March 22, 2016 16:40
Toggle between git branches
# 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.
@hunan-rostomyan
hunan-rostomyan / MapUsingReduce.js
Created March 22, 2016 17:00
Using reduce to define map [JS, Python]
// 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));
}, []);
};
@hunan-rostomyan
hunan-rostomyan / AsyncFor.js
Last active June 21, 2016 17:50
Asynchronous forEach
// 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);
}
@hunan-rostomyan
hunan-rostomyan / called.js
Last active July 11, 2016 22:33
Stateful Functions
var called = (function() {
var count = 0;
return function() {
count++;
console.log(`Called : ${count}`);
};
}());
called(); //=> Called : 1
called(); //=> Called : 2
@hunan-rostomyan
hunan-rostomyan / handler.ts
Last active July 15, 2016 22:16
Type Assertion
// 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"
}
@hunan-rostomyan
hunan-rostomyan / cyclone4.ts
Last active July 15, 2016 22:48
Some ways of organizing dictionaries
// 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'}
];
@hunan-rostomyan
hunan-rostomyan / scan.js
Created July 20, 2016 03:32
Scan (prefix sum, cumulative sum, inclusive scan) in JavaScript
// 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)]
@hunan-rostomyan
hunan-rostomyan / Counter.js
Last active August 26, 2016 23:58
Closures: Javascript vs Python
/*
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;
@hunan-rostomyan
hunan-rostomyan / countingsort.py
Created October 22, 2016 00:21
Ah, Python...
from collections import Counter
def sort(lst):
return [i for i, n in Counter(lst).items() for _ in range(n)]