Skip to content

Instantly share code, notes, and snippets.

View prafulliu's full-sized avatar

Pengfei Liu prafulliu

  • teambition
  • shanghai
View GitHub Profile
@prafulliu
prafulliu / setDefault2.lua
Created December 18, 2012 07:20
setDefault in lua , another version.
local key = {} --unique key
local mt = {__index = function (t) return t[key] end}
function setDefault( t, d )
t[key] = d
setmetatable(t, mt)
end
tab = {x=10, y=20}
@prafulliu
prafulliu / setDefault.lua
Created December 18, 2012 07:19
setDefault in lua.
function setDefault( t, d )
local mt = {__index = function () return d end}
setmetatable(t, mt)
end
tab = {x=10, y=20}
print(tab.x, tab.z)
setDefault(tab, 0)
print(tab.x, tab.z)
@prafulliu
prafulliu / trackingTableAccess2.lua
Created December 18, 2012 07:16
tracking table acceses in lua
local index = {}
-- create private index
local mt = {
__index = function ( t, k )
print("*access to element " .. tostring(k))
return t[index][k] --access the original table
end,
__newindex = function ( t, k, v)
@prafulliu
prafulliu / set.lua
Created December 18, 2012 03:31
A lua metatable demo.
Set = {}
-- create a new set with the values of the
-- given list
local mt = {}
function Set.new(l)
local set = {}
'use strict';
// http://www.infoq.com/cn/presentations/node-js-and-real-time-baas-cloud-development-practice?utm_source=infoq&utm_medium=videos_homepage&utm_campaign=videos_row3
// This is a free list to avoid creating so many same object.
exports.Freelist = function(name, max, constructor) {
this.name = name;
this.constructor = constructor;
this.max = max;
this.list = [];
};
@prafulliu
prafulliu / tree.md
Created July 23, 2012 14:09 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@prafulliu
prafulliu / metatable
Created January 8, 2012 09:46
lua metatable
http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html
http://blog.csdn.net/hong201/article/details/4153182
http://www.cnblogs.com/simonw/archive/2006/12/20/597986.html
@prafulliu
prafulliu / vim config
Created January 6, 2012 09:57
vim config
http://spf13.com/post/ultimate-vim-config
@prafulliu
prafulliu / shuffle lua
Created January 4, 2012 09:58
shuffle
http://www.gammon.com.au/forum/?id=9908
http://blog.fuqcool.com/2011/04/17/algorithm-shuffle.html
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
@prafulliu
prafulliu / linux 批量替换查找
Created January 3, 2012 07:55
批量替换查找
sed -i "s/floating-point/fixed-point/g" `find . -name "*.xml"|xargs grep floating-point -rl`
http://www.blogjava.net/hispark/archive/2010/08/14/328851.html