New in version 0.1.
the fp module provides a collection of functional programming inspired utilities.
this module supplements the core operator, functools, and itertools modules.
Higher-Order Functions
Included with fp are higher-order functions which supplement those provided by functools.
Alias of functools.partial
Returns an prepended partial function which when called will behave like func called with the positional arguments args and keywords
If more arguments are supplied to the call, they are prepended to args. If additional keyword arguments are supplied, they extend and override keywords.
This is useful for converting mapping functions whose first argument is the subject of mapper.
>>> list(map(pp(str.lstrip, '/'), ['/foo', '/bar']))
['foo', 'bar']
This is the equivalence to these expressions
>>> [item.lstrip("/") for item in ['/foo', '/bar']]
['foo', 'bar']
>>> list(map(lambda x: x.lstrip('/'), ['/foo', '/bar']))
['foo', 'bar']
Returns a new function which is the equivalent to f(g(*args, **kwargs))`
Example:
>>> list(map(
... c(str.lower, pp(operator.getitem, "word")),
... [{"word": "Xray"}, {"word": "Young"}]
... ))
['xray', 'young']
Returns a function which always returns x regardless of arguments
>>> const('foo')(1, 2, foo='bar')
'foo'
Calls the method
>>> from six.moves import reduce
>>> reduce(
... p(callreturn, set.add),
... ["a", "b", "c"],
... set()
... ) == set(["a", "b", "c"])
True
Returns a function which applies a dict as kwargs.
Useful for map functions.
>>> def full_name(first=None, last=None):
... return " ".join(
... coalesce([first, last])
... )
...
>>> list(map(
... kwfunc(full_name),
... [
... {"first": "Eric", "last": "Moritz"},
... {"first": "John"},
... {"last": "Cane"},
... ]
... )) == ["Eric Moritz", "John", "Cane"]
True
Optionally, needed keys can be passed in:
>>> list(map(
... kwfunc(full_name, ["first", "last"]),
... [
... {"first": "Eric", "last": "Moritz"},
... {"first": "Gina", "dob": "1981-08-13"},
... ]
... )) == ["Eric Moritz", "Gina"]
True
A function that returns what is passed to it.
>>> identity(1)
1
Iterators
These provide a common set of iterators to supplement the built-in itertools module.
Takes n items off the iterable:
>>> list(itake(3, range(5)))
[0, 1, 2]
>>> list(itake(3, []))
[]
Drops the first n items off the iterator
>>> list(idrop(3, range(6)))
[3, 4, 5]
>>> list(idrop(3, []))
[]
yields two iterators split at index i
>>> def materalize(chunks):
... "Turns a iterator of iterators into a list of lists"
... return list(map(list, chunks))
>>> materalize(
... isplitat(3, range(6))
... )
[[0, 1, 2], [3, 4, 5]]
Zips a function with two iterables
>>> list(izipwith(lambda x,y: (x,y), [1,2], [3,4]))
[(1, 3), (2, 4)]
Reducers
returns True if all elements of the list satisfy the predicate, and False otherwise.
>>> allmap(even, [1, 2, 3])
False
>>> allmap(even, [2, 4, 6, 8])
True
returns True if any of the elements of the list satisfy the predicate, and False otherwise
>>> anymap(even, [1, 2, 3])
True
>>> anymap(even, [1, 3, 7])
False
Predicates
True if x is even
>>> even(1)
False
>>> even(2)
True
True if x is even
>>> odd(1)
True
>>> odd(2)
False