Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

fx - a functional programming approach

 ___
|  _|_ _
|  _|_'_|
|_| |_,_|igher-order function coding.

Introduction

TL;DR - YAGNI.

Inspired by Haskell's rich set of operators, this is an approach to functional programming with operators in Python.

"It's fun... It's insane... It's insanely fun."

—John Doe

Features

  • Currying functions with <<, &
  • Piping output of functions with |
  • Composing functions with **
  • Flipping order of arguments of function with ~
  • and more

Examples

>>> 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

Requirements

  • CPython >= 2.6

Installation

Install from PyPI:

pip install fx

Install from source, download source package, decompress, then cd into source directory, run:

make install

License

BSD New, see LICENSE for details.

Recent activity

Philip Xu

Commits by Philip Xu were pushed to pyx/fx

ff0efbc - Fixed test_compose. Replaced function 'id' with function 'hash'. The id of string objects with same value might or might not be the same, depending on ...
Philip Xu

Commits by Philip Xu were pushed to pyx/fx

f4792fb - Improved test_compose. - covers all permutations of f, g, h compositions - uses 'type' instead of 'hash' as function h, 'hash' acts as identity function ...
Philip Xu

Commits by Philip Xu were pushed to pyx/fx

a8f04dc - Moved helper functions into separate module.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.