Skip to content

Latest commit

 

History

History
55 lines (37 loc) · 1.65 KB

fp.md

File metadata and controls

55 lines (37 loc) · 1.65 KB

Functional Programming in Python

GvR seems to somewhat hate the FP list idioms, but at least functions are first class objects and he didn't totally wipe out the few basic FP functions in Python 3.

Standard Library

The standard library includes category of functional programming modules:

  • itertools: Functions creating iterators for efficient looping.
  • functools: Higher-order functions and operations on callable objects.
  • operator: Function versions of operators for higher-order use.

functools Highlights:

  • partial(func, *args, **keywords): Returns a partial object (attributes: func, args, keywords) called as a function:

    drop1A = partial(re.sub, 'A', '', count=1)
    drop1A('AbAb') == 'bAb'
    
  • partialmethod: Used for method definitions in objects:

    class Foo():
        def id(x): return x
        three = partialmethod(id, 3)
    
  • reduce(function, iterable[, initializer])

toolz

Composable, pure and lazy tools for list processing as done in functional languages.

See: toolz-pypy, toolz-docs, tools-github