Skip to content

Instantly share code, notes, and snippets.

public class N {
N prev;
N(N a) {
this.prev = a;
}
N addOne(N a) {
return new N(a);
}
"open A_test.X if current file name is A.X
"open A.X if current file name is A_test.X
function OpenCorrespondingFile()
let d = split(expand("%"), '_test')
if len(d) == 1
let name = expand("%:r") . "_test." . expand("%:e")
else
let name = d[0] . d[1]
endif
exec 'vsplit ' name
#!/usr/bin/env python2.5
if __name__ == '__main__':
pass
@fcamel
fcamel / attribute.py
Created February 15, 2011 15:18
attribute and descriptor
class X(object):
property = property(fget=lambda x: "property value")
direct_attribute = "direct_attribute"
def method(self):
return "method value: %s" % id(self)
@classmethod
def classmethod(self):
return "classmethod value: %s" % id(self)
x.__dict__
{}
x.property
------------------------------------------------------------
x.property property value
type(x).__dict__['property'] <property object at 0x800fc9a48>
type(x).__dict__['property'].__get__(x, type(x)) property value
x.direct_attribute
@fcamel
fcamel / str_list.sh
Created June 8, 2011 17:32
prepare "a list of strings" in shell script
files=$(cat <<EOF
1st line
2nd line
3rd line
EOF
)
IFS=$'\n'
for f in $files
do
@fcamel
fcamel / call_main.java
Created June 10, 2011 11:40
Use reflection to call main method
static boolean callMain(String className, String[] mainArgs) {
try {
Class cls = Class.forName(className);
Class[] parameterTypes = new Class[1];
parameterTypes[0] = String[].class;
Method main = cls.getMethod("main", parameterTypes);
Object[] args = new Object[] { mainArgs };
main.invoke(null, args);
} catch (Throwable e) {
System.err.println(e);
@fcamel
fcamel / sleep_sort.py
Created June 23, 2011 16:09
Sleep Sort: an amazing sorting algorithm!!
#!/usr/bin/env python2.6
# -*- encoding: utf8 -*-
'''
An advanced version of Sleep Sort.
The original idea is from: http://coolshell.cn/articles/4883.html
Use the concept of radix sort to speed it up.
The newer version references WanCW's implementation in Ruby:
@fcamel
fcamel / JumboEnumSet.java
Created July 3, 2011 02:59
JumboEnumSet.add
public boolean add(E e) {
typeCheck(e);
int eOrdinal = e.ordinal();
int eWordNum = eOrdinal >>> 6;
long oldElements = elements[eWordNum];
elements[eWordNum] |= (1L << eOrdinal);
boolean result = (elements[eWordNum] != oldElements);
if (result)
@fcamel
fcamel / split_trim.js
Created July 14, 2011 03:30
jquery: split + trim
f = $("<div>a, b, c</div>"); // [<div>​a, b, c​</div>​]
$.map(f.html().split(","), $.trim); // ["a", "b", "c"]