Skip to content

Instantly share code, notes, and snippets.

@rctay
rctay / bashrc
Created November 14, 2011 07:40 — forked from shawntan/bashrc
[fork] my .bashrc
# /etc/bash/bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
@rctay
rctay / gist:1361698
Created November 13, 2011 06:37
[ruby] 40 stones - a brute-force solution
# http://beust.com/weblog/2011/10/30/a-new-coding-challenge/
require 'set'
def cut(a, n)
if a < n+1
return nil
elsif n <= 0
return [[a]]
elsif a == n+1
@rctay
rctay / appender3.xml
Created November 2, 2011 13:41 — forked from iwein/appender3.xml
[fork] Appender filtering particular trace logging
<appender name="STDOUT2" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<OnMatch>NEUTRAL</OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>STDOUT1 %logger{36} - %msg%n</Pattern>
</layout>
</appender>
@rctay
rctay / jk-flipflop.js
Created October 31, 2011 15:03
[js] 2-bit counter with JK flip-flops
function flip(bit) {
return bit ? 0 : 1;
}
// each call simulates clock tick
function jk() {
var q=0;
return function(j, k) {
if (j) {
if (k)
@rctay
rctay / gist:1244436
Created September 27, 2011 05:58
[git] dump all files
# dumps files in the format "file: <name>\n<file contents>"
$ git ls-tree --name-only -r HEAD | awk '{ print "echo file: "$0"; cat "$0; }' | sh
@rctay
rctay / gist:1241525
Created September 26, 2011 02:59
[java] invoking instance methods with Reflection
// invoking inst.getYear(void)
inst.getClass().getMethod("getYear", void.class).invoke(t1, void.class));
@rctay
rctay / bogosqrt.c
Created September 23, 2011 09:50
xdiff's bogosqrt
/* from libxdiff's xutils.c */
long xdl_bogosqrt(long n) {
long i;
/*
* Classical integer square root approximation using shifts.
*/
for (i = 1; n > 0; n >>= 2)
i <<= 1;
@rctay
rctay / gist:1226563
Created September 19, 2011 14:05 — forked from qoelet/gist:1225838
[fork] currying in python (from http://pydroid.posterous.com/nextpy-currying)
"""
See also:
- http://code.activestate.com/recipes/52549/#c6
- http://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application
"""
class curry:
def __init__(self, fun):
import inspect
self.fun = fun
@rctay
rctay / L.c
Created September 16, 2011 12:51
CS2104 interpreter
#include <ctype.h>
#include <malloc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* utilities - shamelessly lifted from Git */
void vreportf(const char *prefix, const char *err, va_list params)
{
@rctay
rctay / extract1.xml
Created August 4, 2011 18:22
[ant] a tale of two Selectors
<copy todir="${temp_dir}/tinymce">
<fileset dir=".">
<include name="**"/>
<exclude name="**/.*/**"/>
<exclude name="**/.*"/>
<exclude name="build-utils/**" />
<!-- let the other FileSet take care of it -->
<exclude name="jscripts/tiny_mce/themes/advanced/js/*" />
</fileset>