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 / 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 / UniformGuard.py
Last active September 28, 2020 01:50
Implementing Guards using Decorators in Python
"""
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:
@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 / Scheduler.js
Last active September 30, 2021 06:12
A setTimeout with delay
// 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);
}
@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'}
];