fx-函数式编程方法
fx的Python项目详细描述
___ | _|_ _ | _|_'_| |_| |_,_|igher-order function coding.
简介
tl;博士-雅格尼。
受到Haskell丰富的运算符集的启发, 这是一种使用Python中的运算符进行函数式编程的方法。
“It’s fun… It’s insane… It’s insanely fun.”
—John Doe
功能
- 使用<<,&
- 使用| 管道输出函数
- 使用** 组合函数
- 使用~ 函数参数的翻转顺序
- 更多
示例
>>> from fx import f >>> double_all = f(map) << 2 .__mul__ | list >>> double_all([1, 2, 3]) [2, 4, 6] >>> double_all |= f(map) << str | ' '.join >>> double_all([1, 2, 3]) '2 4 6' >>> sum_upto = 1 .__add__ | f(range) << 1 | sum >>> sum_upto(100) 5050 >>> parse_hex_str = ~f(int) << 16 >>> parse_hex_str('ff') 255 >>> parse_hex_str('c0ffee') 12648430 >>> # project euler problem 1 >>> euler_p1 = f(range) << 1 | f(filter) << (lambda n: n % 3 == 0 or n % 5 == 0) | sum >>> euler_p1(10) 23 >>> euler_p1(1000) 233168 >>> # project euler problem 20 >>> fact = f(lambda n: 1 if n == 1 else n * fact(n - 1)) >>> euler_p20 = str ** fact | sum ** f(map) << int >>> euler_p20(10) 27 >>> euler_p20(100) 648
要求
- cpython=2.6
安装
从pypi安装:
pip install fx
从源安装,下载源包,解压缩,然后cd到源目录,运行:
make install
许可证
bsd new,请参阅许可证以了解详细信息。
链接
- 文档:
- http://fx.readthedocs.org/
- 问题跟踪器:
- https://bitbucket.org/pyx/fx/issues/
- 源包@pypi:
- http://pypi.python.org/pypi/fx/
- Mercurial存储库@BitBucket:
- https://bitbucket.org/pyx/fx/
- git存储库@github:
- https://github.com/pyx/fx/