wyc's domain

Code Snippets

Some are by me, most are by others.

DO (C)

typedef long I;
#define R return
#define DO(n,x) {I i=0,_n=(n);for(;i<_n;++i){x;}}
mv(d,s,n)I *d,*s;{DO(n,d[i]=s[i]);}
tr(r,d)I *d;{I z=1;DO(r,z=z*d[i]);R z;}


regex precursor to sed-like substitution (coffeescript)

# s/<query>/<replacement>/<flags>
# <user1> you're a luser
# <user2> user1: s/luser/winnar/

@pattern = /^(?:([^\s]+)?(?:: ))?s\/([^\/\\]*(?:\\.[^\/\\]*)*)\/([^\/\\]*(?:\\.[^\/\\]*)*)(?:\/(.*))?/
target = msg.match[1]
query = msg.match[2].replace /\\\//g, '/'
replace = msg.match[3].replace /\\\//g, '/'
flags = msg.match[4]

get struct's base address from a member name (C)
linux kernel 2.4.24: include/linux/list.h

 * list_entry - get the struct for this entry
 * @ptr:        the &struct list_head pointer.
 * @type:        the type of the struct this is embedded in.
 * @member:        the name of the list_struct within the struct.
#define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

write to open file requiring root permissions (vim)

:w !sudo tee %

to be prudent:
:w !sudo tee % > /dev/null

vimscript binding to :W in the .vimrc:
command W w !sudo tee % > /dev/null

get nth return value (Go)

Tim Schaeffer   

You could also be more abstract and create some higher-order functions.
This solves the problem more generally:

func make_nth (n int) (func(a... interface{}) interface{}) {
         return func(args... interface{}) interface{}{
                 return args[n]

var first = make_nth(0)
var second = make_nth(1)
var third = make_nth(2)
// etc.

var dir string = first(path.Split(os.Args[0])).(string)