将常用函数式编程操作作为方法注入可iterable对象的库。
funcip的Python项目详细描述
作为方法注入通用函数式编程操作的库 变成可接受的物体。目前,它注射 filter, flat_map, foldl, foldr, map,和reduce操作 使用sum, count, any, all, max, min,和zip操作。
获取库
可以通过pip install funcipy安装库。同样,它 可以通过pip uninstall funcipy卸载。
用法
当您希望将公共函数编程操作作为 方法进入对象obj,调用funcipy.funcify函数 obj作为参数。如果obj是iterable,那么函数将 返回一个对象,该对象
- 提供与输入对象相同的接口,并且
- 将函数式编程操作作为方法。
否则,obj将按原样返回。
下面是一些调用示例。
fromfuncipyimportfuncifyimportfunctoolsimportitertoolsimportoperatori=range(5,15)print("Map function: "+' '.join(map(str,i)))tmp1=funcify(i)print("Map function applied to funcified object: "+' '.join(map(str,tmp1)))print("Map method: "+' '.join(tmp1.map(str)))print("Map and Filter Method chaining: "+' '.join(tmp1.filter(lambdax:x%2).map(str)))print("Reduce function: "+str(functools.reduce(operator.add,i,5)))print("Reduce method: "+str(tmp1.reduce(operator.add,5)))print("Reduce function: "+str(functools.reduce(operator.sub,i)))print("Foldl method: "+str(tmp1.foldl(operator.sub)))print("Foldr method: "+str(tmp1.foldr(operator.sub)))print("Flat-map operation: "+' '.join(itertools.chain.from_iterable(map(str,i))))print("Flat-map method: "+' '.join(funcify(i).flat_map(str)))print("Sum function: "+str(sum(i)))print("Sum method: "+str(tmp1.sum()))print("Count function: "+str(sum(1for_infilter(lambdax:x>10,i))))print("Count method: "+str(tmp1.count(lambdax:x>10)))print("Max function: "+str(max(i)))print("Max method: "+str(tmp1.max()))print("Min function: "+str(min(i)))print("Min method: "+str(tmp1.min()))print("Any function: "+str(any(map(lambdax:x>10,i))))print("Any method: "+str(tmp1.map(lambdax:x>10).any()))print("All function: "+str(all(map(lambdax:x>10,i))))print("All method: "+str(tmp1.map(lambdax:x>10).all()))j=range(0,7)print("Zip function: "+' '.join(map(str,zip(i,j))))print("Zip method: "+' '.join(tmp1.zip(j).map(str)))
归因
版权所有(c)2017,Venkatesh Prasad Ranganath
根据BSD 3条款“新”或“修订”许可 (https://choosealicense.com/licenses/bsd-3-clause/)
作者:venkatesh prasad ranganath