Skip to content

Instantly share code, notes, and snippets.

View kavinyao's full-sized avatar

Kavin Yao kavinyao

View GitHub Profile
@kavinyao
kavinyao / mt_rand_vs_rand.php
Created May 21, 2012 11:46
PHP: mt_rand vs rand
<?php
$range_count = array_fill(0, 100, 0);
$start_time = microtime(true);
for($i = 0;$i < 1000000;++$i) {
$randint = mt_rand(0, 999);
$range_count[$randint/10]++;
}
$end_time = microtime(true);
$duration1 = $end_time - $start_time;
@kavinyao
kavinyao / ObserverExample.java
Created June 23, 2012 06:59
Observer Pattern Example
import java.util.*;
/*
* Example of Observer Pattern:
* Student is the model
* View1 and View2 display information of the student
* View3 changes information of the student
* Whenever the student is changed, the views displaying it should be updated
*/
@kavinyao
kavinyao / LimitedInstances.java
Created June 23, 2012 13:11
A solution to limited instances problem using Proxy Pattern
import java.util.LinkedList;
import java.util.List;
/*
* A solution using Proxy Pattern to limited instances problem
* Requirement:
* class RareResource must have no more than 3 instances at any run time.
* Note:
* Black magic like reflection is out of consideration as you can never prevent it.
*/
@kavinyao
kavinyao / average_status_length.js
Created June 30, 2012 01:34
Calculate the average status length in current Fanfou page
/**
* What's the average status length in current Fanfou page?
* Author: @kavinyao
* MIT-licensed.
*/
(function(document) {
function map(op, seq) {
var result = []
for(var i = 0;i < seq.length;i++)
result.push(op(seq[i]))
@kavinyao
kavinyao / gist:4014994
Created November 5, 2012 02:38 — forked from clippit/gist:4012541
strstr
#include <stdio.h>
const char* my_strstr(const char* haystack, const char* needle) {
if (!*needle)
return haystack;
const char* h = haystack;
const char* n = needle;
while (*h) {
const char* hp = h;
@kavinyao
kavinyao / find_bom.fish
Last active December 16, 2015 14:39
find js file with UTF-8 BOM recursively, using fish shell
for f in (find . -name '*.js')
head -1 $f | xxd | grep 'efbb bf' > /dev/null
# if matched a line, $status == 0
if test $status -eq 0
echo $f
end
end
@kavinyao
kavinyao / promisify.coffee
Created May 17, 2013 10:19
Practice code to help understand promise.
# Demo code in coffeescript for Callbacks are imperative, promises are functional: Node’s biggest missed opportunity
# http://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/
# the original version uses rsvp.Promise
# since I'm a control freak, I'd prefer Deferred
Deferred = require 'deferred'
slice = Array.prototype.slice
# transform a callback-based function to return a promise
# fn == function(param..., function(error, data...))
# This piece of code is a rebound of https://github.com/ForbesLindesay/curry
# Author: Kavin Yao <kavinyao@gmail.com>
# License: MIT
import inspect
def curry(func, num_params=-1):
"""Simple, unlimited curry.
The curried function is not called until desired number of arguments is passed.
Note: **kwarg is not supported
"""
import sys
import math
import time
def progress_bar(progress, col_width=80):
"""
Text progress bar in command line.
Pre-condition: the cursor is at a new line.
Post-condition: the cursor is at the end of the same line.
@kavinyao
kavinyao / bst.py
Created November 14, 2013 20:06
Rudimentary BST in Python.
class Node(object):
"""A node in BST."""
def __init__(self, value, left_node=None, right_node=None):
self.value = value
self.left_node = left_node
self.right_node = right_node
def set_left(self, left_node):
self.left_node = left_node