python函数式编程

来源:互联网 发布:电子教室软件免费版 编辑:程序博客网 时间:2024/05/21 05:57

直接上代码,类似与swift
#! /usr/bin/env python
#def add(x,y,f):#    return f(x)+f(y);#print (add(-5,-10,abs));#def format_name(s):#    return s[0].upper() + s[1:].lower();#print map(format_name,['adam', 'LISA', 'barT'])#def prod(x, y):#    return x*y#print reduce(prod, [2, 3,4, 5],10)#import math#def is_sqr(x):#    r = int(math.sqrt(x))#    return r*r==x#print filter(is_sqr, range(1, 101))def is_not_empty(s):    return s and len(s.strip()) > 0print filter(is_not_empty, ['test', None, '', 'str', '  ', 'END'])

"""def cmp_ignore_case(s1, s2):    u1 = s1.upper()    u2 = s2.upper()    if u1 < u2:        return -1    if u1 > u2:        return 1    return 0print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)"""

原创粉丝点击