Skip to content

Instantly share code, notes, and snippets.

@prisoner
prisoner / canvas1.html
Created July 28, 2011 17:42
HTML5 Canvas Sample
<!DOCTYPE HTML>
<html>
<head>
<title>HTML5 Canvas Sample</title>
<!--[if IE]><script src="excanvas.js"></script><![endif]-->
<script>
//<![CDATA[
function drawRect() {
var canvas = document.getElementById("canvas");
@prisoner
prisoner / consistent_hashr.rb
Created July 28, 2011 18:05
A Consistent Hashing implementation for Ruby
require 'zlib'
module ConsistentHashr
@circle = {}
@number_of_replicas = 20
##
# Computes a key
def self.hash_key(key)
Zlib.crc32("#{key}")
@prisoner
prisoner / c1312.Observable.js
Created July 28, 2011 18:08
observer pattern for js
var c1312 = c1312 || {};
c1312.Observable = function() {
this._observers = [];
}
c1312.Observable.prototype = {
addObserver: function(o) {
this._observers.push(o);
},
@prisoner
prisoner / c1312.Observable.qunit.js
Created July 28, 2011 18:09
c1312.Observable.qunit
module("c1312.Observable");
test("c1312.Observable 测试", function() {
var subject = new c1312.Observable();
var observer_count = 0;
var o1 = function(msg) {
observer_count ++;
equals(msg, 'hello', 'notifyObservers操作1');
}
@prisoner
prisoner / binary_literals.java
Created July 29, 2011 07:55
Binary Literals in Java7
// An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.
@prisoner
prisoner / strings_in_switch_statements.java
Created July 29, 2011 07:58
Strings in switch Statements
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
@prisoner
prisoner / try-with-resources.java
Created July 29, 2011 08:05
The try-with-resources Statement
public static void viewTable(Connection con) throws SQLException {
String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
@prisoner
prisoner / underscores_in_numeric_literals.java
Created July 29, 2011 08:21
Underscores in Numeric Literals
//正确
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
@prisoner
prisoner / hello.go
Created August 7, 2011 13:00
hello Go
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
#compdef go
_arguments "1:commands:((\
build\:'compile packages and dependencies' \
clean\:'remove object files' \
doc\:'run godoc on package sources' \
env\:'print Go environment information' \
fix\:'run go tool fix on packages' \
fmt\:'run gofmt on package sources' \
get\:'download and install packages and dependencies' \