{{ message }}

Instantly share code, notes, and snippets.

# Hunan Rostomyan hunan-rostomyan

Created Mar 6, 2016
Defining Recursive Functions using a Fixed Point Combinator
View FixedFactorial.js
 /* 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;}
Last active Mar 22, 2016
Toggle between git branches
View gitback.sh
 # 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.
Created Mar 22, 2016
Using reduce to define map [JS, Python]
View MapUsingReduce.js
 // 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)); }, []); };
Last active Jun 21, 2016
Asynchronous forEach
View AsyncFor.js
 // 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); }
Last active Jul 11, 2016
Stateful Functions
View called.js
 var called = (function() { var count = 0; return function() { count++; console.log(`Called : \${count}`); }; }()); called(); //=> Called : 1 called(); //=> Called : 2
Last active Jul 15, 2016
Type Assertion
View handler.ts
 // 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" }
Last active Jul 15, 2016
Some ways of organizing dictionaries
View cyclone4.ts
 // 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'} ];
Created Jul 20, 2016
Scan (prefix sum, cumulative sum, inclusive scan) in JavaScript
View scan.js
 // 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)]
Last active Aug 26, 2016
Closures: Javascript vs Python
View Counter.js
 /* 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;
Created Oct 22, 2016
Ah, Python...
View countingsort.py
 from collections import Counter def sort(lst): return [i for i, n in Counter(lst).items() for _ in range(n)]