Contents:
Tiny functional programming oriented Python library inspired by underscore.js and Haskell prelude.
Takes a two or more functions and returns a composition of them:
>>> fn = compose(lambda x: x/2, lambda x: x+5)
>>> fn(5)
5.0
Returns a function that can be partially applied:
>>> fn = curry(lambda x,y: x**y)
>>> fn(2,8)
256 >>> fn(2)(8) 256
Returns a function with some of its arguments bound:
>>> fn = template(lambda a,b,c: (a*b)/c, ___, 2, ___)
>>> fn(5, 2)
5.0
A subclass of list that changes the behaviour of append, extend, insert, remove, reverse and sort so that they allow chaining of method calls:
>>> chain([1,2,3]).append(4).reverse().extend([5, 6]).sort()
[1,2,3,4,5,6]
Returns a function that takes an object and calls its method optionally passing it *args and **kwargs
Invokes method on every member of the iterable passing it *args and **kwargs
An iterator that repeatedly applies fn to x
A decorator that checks that all of the provided preconditions are met when the function is called:
>>> @pre(lambda x: x > 0)
>>> def fn(x):
>>> return x*x
>>> fn(-1)
A decorator that checks that all of the provided postconditions are met when the function returns
A function that takes one argument and returns it.
A function that takes a function with arity of two and returns a function with the arguments flipped
Takes an iterable and returns its first item
Takes and iterable and returns its second item
Raised when pre- or postcondition fails