Skip to content

Instantly share code, notes, and snippets.

View kavinyao's full-sized avatar

Kavin Yao kavinyao

View GitHub Profile
puts "Solution to 1:"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num}
puts
puts "Solution to 2:"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each { |num| puts num if num > 5 }
puts
puts "Solution to 3:"
odds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].select {|num| num % 2 == 1 }
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Demo of exception handling in ExecutorService.
*/
public class ExecutorServiceExceptionDemo {
/**
#!/usr/bin/env python3
from enum import Enum
from random import choice
def 做(事情):
事情()
def 如果(条件成立, 那么做, 动作1, 不然就做, 动作2):
if 条件成立:
@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 / 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 / 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.