Skip to content

Instantly share code, notes, and snippets.

View kenchangh's full-sized avatar
🎀

Ken Chan kenchangh

🎀
  • World
View GitHub Profile
@kenchangh
kenchangh / main.js
Last active March 28, 2016 02:14
webworker
/*
Question: I need to access to the web worker's onmessage data somehow
*/
var worker = new Worker('worker.js');
var file = {
createReadStream: function() {
worker.postMessage(options); // some sort of options
worker.onmessage = function(e) {
@kenchangh
kenchangh / approx.py
Created March 15, 2016 14:09
Approximate exponent using continuous fractions
def approximate_exponent(end):
denominator = float(end) + 1
for i in xrange(end, 0, -1):
denominator = i + i/denominator
return 2+1/denominator
print approximate_exponent(2) # 2.72727272727
print approximate_exponent(3) # 2.71698113208
@kenchangh
kenchangh / rAF.js
Created June 25, 2015 11:27 — forked from paulirish/rAF.js
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animatin
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
@kenchangh
kenchangh / linked_list.c
Created March 5, 2015 11:11
An implementation for linked list in C. Just to polish up my C skills.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int value;
struct node *next;
} node;
node *add_item(node *head, int value) {
node *new_node = malloc(sizeof(node));
@kenchangh
kenchangh / asyncChecker.js
Last active August 29, 2015 14:09
Checks if asynchronous function is finished
function asyncChecker(condition, callback, interval) {
var checkerInterval = setInterval(function() {
if (condition) {
clearInterval(checkerInterval);
callback();
}
}, interval);
}
@kenchangh
kenchangh / internalize.js
Last active February 15, 2021 18:08
Injecting contents of external scripts into HTML string
// Implementation
function internalizeTextFiles(type, html, callback) {
var tagNames = {
js: 'script',
css: 'style'
}
var tagName = tagNames[type];
var $html = $(html);
var $assets = $html.filter(tagName);
var fetchedCounter = 0;
@kenchangh
kenchangh / docstring.py
Created October 11, 2014 07:06
An example on "Googley" docstring
def public_fn_with_googley_docstring(name, state=None):
"""This function does something.
Args:
name (str): The name to use.
Kwargs:
state (bool): Current state to be in.
Returns:
@kenchangh
kenchangh / setup-ubuntu.sh
Created September 5, 2014 07:12
Setting up Python development environment in Ubuntu
sudo apt-get update
sudo apt-get install build-essential python-dev
sudo apt-get install make wget
# Redis
wget http://download.redis.io/releases/redis-2.8.14.tar.gz
tar xzf redis-2.8.14.tar.gz
cd redis-2.8.14
make
sudo make install
@kenchangh
kenchangh / .bashrc
Created September 4, 2014 08:14
My .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac