Skip to content

Instantly share code, notes, and snippets.

@joyeecheung
joyeecheung / resolve.js
Created August 28, 2014 07:30
A simplified version of the require.resovle() from node.js
var path = require('path');
// require(X) from module at path Y
function resolve(x, y) {
// 1. If X is a core module,
// a. return the core module
// b. STOP
if (isCoreModule(x)) {
console.log(x + " is a core module");
}
@joyeecheung
joyeecheung / reverse.js
Created September 5, 2014 04:12
Reverse a list recursively.
function reverse(a) {
return a.length ? reverse(a.slice(1)).concat([a[0]]) : [];
}
@joyeecheung
joyeecheung / draggable.js
Created September 10, 2014 10:07
A simple drag and drop plugin, supporting IE8+, Firefox, Chrome
;(function(window, undefined) {
/**
* some utitlity to help handling compatibility issues
*/
var util = {
/**
* Cross-browser event handler support.
* @param {Object} elem the event target
* @param {Object} type event object
* @param {Function} fn event handler
@joyeecheung
joyeecheung / readUntil.c
Last active August 29, 2015 14:06
Read until the character `?`
#include <stdio.h>
#include <string.h>
int main(void) {
char line[30000];
char str[30000];
FILE * pFile = fopen ("myfile.txt" , "r");
/* Avoid buffer overflow by using fgets() */
@joyeecheung
joyeecheung / volatility.py
Created September 30, 2014 11:17
Python script for jade's quant case
import math
data = list()
f = open("data")
for line in f:
data.append(float(line))
T = len(data) - 1
@joyeecheung
joyeecheung / inputString.c
Created October 14, 2014 06:31
read a string unitl a newline character is entered
#include <stdio.h>
int main(int argc, char *argv[]) {
char msg[100];
char *in = "%99[^\n]%*c";
scanf(in, msg);
printf("%s\n", msg);
return 0;
}
@joyeecheung
joyeecheung / reverseChild.js
Last active August 29, 2015 14:07
Reverse the children of an DOM Element while preserving their CSS styles and event listeners. Due to their incorrect implementation of `children`, this might no work in IE8-.
function reverseChild(pare) {
if (!pare.children || pare.children.length < 2) return;
var emptyParent = document.createElement(pare.nodeName);
var detachedParent = pare.parentNode.replaceChild(emptyParent, pare);
var emptyLeft = document.createElement(detachedParent.children[0].nodeName);
var emptyRight = document.createElement(detachedParent.children[0].nodeName);
for (var i = 0, len = detachedParent.children.length; i < Math.floor(len/2); i++) {
var left = detachedParent.replaceChild(emptyLeft, detachedParent.children[i]);
@joyeecheung
joyeecheung / vla-forward.c
Created November 27, 2014 18:17
Variable length array as function parameter, casted before passing, using GNU C forward parameter declaration
// compile me with `gcc -std=c99` or `gcc -std=gnu99`
#include <stdio.h>
int sum(int n_col; int arr[][n_col], int n_row, int n_col) {
int i, j, total = 0;
for (i = 0; i < n_row; ++i) {
for (j = 0; j < n_col; ++j) {
total += arr[i][j];
}
@joyeecheung
joyeecheung / reverse.js
Created January 7, 2015 04:49
Reverse a sequence(array or string) recursively.
function reverse(seq) {
if (seq.length > 1)
return seq.slice(-1).concat(reverse(seq.slice(0, -1)));
else
return seq;
}
@joyeecheung
joyeecheung / chromium-issue-tracker-user-style.css
Last active February 18, 2017 18:01
User style for chromium issue tracker
.issue_text {
font-family: "CamingoCode", "Fira Mono", menlo, monaco, consolas, monospace;
margin-left: 10px;
}
a:link,
a:focus {
color: #1281c9;
}
a:visited {
color: #673ab7;