Skip to content

Instantly share code, notes, and snippets.

View benwei's full-sized avatar

ben6 benwei

View GitHub Profile
@benwei
benwei / regex_fun.js
Created November 27, 2012 11:11
convert function to that.func (javascript)
// regexp
// convert
// function a (a1,a2)
// that.a = function (a1, a2)
cat jsfile.js | sed -e 's/^\([ \t]*\)function[ \t]*\([a-zA-Z]*\)(/\1that.\2 = function (/g'
@benwei
benwei / yd
Created November 27, 2012 10:19
yd script is a simple wrapper while cache read word for ydict
#!/bin/sh
YD_HIST_DIR="${HOME}/.ydict_history"
mkdir -p "$YD_HIST_DIR/_relation/"
echo "$@" >> "${YD_HIST_DIR}"/_words.txt
WordCacheFile="${YD_HIST_DIR}"/"$@".txt
WordRelationFile="${YD_HIST_DIR}"/_relation/"$@".txt
if [ ! -f "$WordCacheFile" ]; then
ydict -u -w "$@" | sed -e "s/'/'/g" -e "s/\(.\[36m\)[ ]*/\1 /g" > "$WordCacheFile"
@benwei
benwei / closure.py
Created July 3, 2012 15:29
closure sample h4
# reference:
# http://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x
# http://en.wikipedia.org/wiki/Closure_(computer_science)#Example
#
def counter():
d = {'x': 0} # tested with python 2.7.2
def increment(y=1):
# nonlocal only work for python3
#nonlocal x
d['x'] += y
@benwei
benwei / makefile
Created February 3, 2012 02:57
membuddy.c & makefile
#http://en.wikipedia.org/wiki/Buddy_memory_allocation
#This is a dummy code for Buddy memory allocation implementation.
CFLAGS=-DHAVE_DEV_SYS
LDFLAGS=
CFILES=membuddy.c
OBJS=$(CFILES:.c=.o)
BINNAME=membuddy
all: $(BINNAME)