Skip to content

Instantly share code, notes, and snippets.

@DamonHao
DamonHao / convert_to_wchar.cpp
Created July 1, 2015 02:00
c++ convert char* to wchar*
char* errorInfo = (char*) errorBuffer->GetBufferPointer();
const size_t cSize = strlen(errorInfo)+1;
wchar_t* wc = new wchar_t[cSize];
size_t tmp = 0;
mbstowcs_s(&tmp, wc, cSize, errorInfo, cSize);
@DamonHao
DamonHao / hello_world.py
Created May 9, 2015 10:44
程序的入口的模块会有两个名字,一个叫 __main__ ,一个叫 hello_world。 所以class A 会存在两个模块里。
# -*- coding: utf-8 -*-
__author__ = 'Administrator'
import test
import sys
class A(object):
haha = []
@DamonHao
DamonHao / default_params.py
Created May 2, 2015 06:58
python default parameters
""" 在python里,默认参数值都是由函数进行引用的,所以前三个和最后一个函数调用使用的都是同一个默认的参数值"""
def foo(item, items=[], added=True):
if added:
items.append(item)
print items
foo(1)
foo(2)
foo(3, added=False)
@DamonHao
DamonHao / Module.lua
Created January 28, 2015 09:19
propose a better way to write lua module
--[[
Lua is an interpreted language. So a variable(like function)
will be acessed only when it is executing. And it is better to write
a Module with a form of case three, because it has no need to do forward declaration
]]
-- case 1
-- local fun2
local function fun()
fun2() -- error, will call global fun2 not local fun2, and it is nil
@DamonHao
DamonHao / call_method.lua
Last active August 29, 2015 14:12
OO programming in Lua
--[[ 3 ways to call an instance's method ]]
Account = {balance = 0}
-- case 1
function Account.withdraw (v)
Account.balance = Account.balance - v
end
-- case 2
function Account.withdraw (self, v)
self.balance = self.balance - v
@DamonHao
DamonHao / class.lua
Created December 31, 2014 02:25
create object's filed secretly when use class concept in lua
--[[
in b:deposit(), b.balance = b.balance + v.
In the left of equation, it mean initialize the file balance of object b,
and will not access the file in Account. but the right side will at the first.
After this, both side will access the filed in object b.
--]]
Account = {balance = 0}
function Account:deposit (v)
self.balance = self.balance + v
@DamonHao
DamonHao / permu_with_coroutine.lua
Created December 27, 2014 07:31
Lua, permutation with coroutine
local function printResult(a)
for i = 1, #a do
io.write(a[i], " ")
end
io.write("\n")
end
local function permgen (a, n)
n = n or #a
if n <= 1 then
@DamonHao
DamonHao / test_routine.lua
Last active August 29, 2015 14:12
the usage of resume and yield
local function testRoutine()
co = coroutine.create(function (x)
for i = 1, 10 do
-- print("co", i)
coroutine.yield(i)
end
end)
print(coroutine.resume(co))
print(coroutine.resume(co))
print(coroutine.resume(co))
@DamonHao
DamonHao / load_code.lua
Last active August 29, 2015 14:11
note the difference between the loaded code(string) and the source code
local function main()
i = 32
local i = 0
f = load(" i = i + 1; print(i)")
g = function () i = i + 1; print(i) end
f()
g()
end
main()
@DamonHao
DamonHao / global_and_local.lua
Created December 22, 2014 02:59
Local and global variable
function test()
x = 1 -- default is global
local y = 2
end
local function main()
test()
print(x, y)
end