# coding:utf-8

# 函数式编程


def add(a, b):
    return a + b


def fprintf(fmt, *args):
    """args is tuple"""
    print fmt % args

print "{0:s}".format("ljinshuan")
fprintf("%s,%d,%s", "xxx", 1, "gerg")


def fprintf(fmt, **args):
    """args is map"""
    print args

fprintf("{name} ,{age}", name="ljinshuan", age=10)

# 装饰器
event_handers = {}


def eventhander(event):
    def register_function(func):
        event_handers[event] = func
        return func
    return register_function


@eventhander("click")  # 返回一个参是handle_click(msg)函数的函数
def handle_click(msg):
    print msg
event_handers["click"]("heheheh")
# just lick
temp = eventhander("click")
handle_click = temp(handle_click)

# 生成器


def countdown(n):
    print "Begin Counting"
   
> 0:
        yield n
        n -= 1
c = countdown(10)
for x in c:
    print x,

# 协程


def receiver():
    print "Begin receiver"
    while True:
        n = (yield)
        print "Got %d" % n
r = receiver()
r.next()
r.send(1)
r.close()


# 接收的同时返回一个值
def line_splitter(delimter=None):
    print "Ready to split"
    result = None
    while True:
        line = (yield result)
        result = line.split(delimter)

s = line_splitter(",")
s.next()
print s.send("A,B,C")
print s.send("100,200,300")
s.close()

# 列表推导
nums = [1, 2, 3, 4, 5]
squars = [x ** 2 for x in nums if x % 2 is not 0]
print squars
# 生成器表达式
squars2 = (x ** 2 for x in nums if x % 2 is not 0)

print squars2.next()


print eval("2 + 3")
exec("for x in xrange(1, 10):print x,")

 while n 