fp — A collection of functional programming inspired utilities

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.

fp.p(func : callable[, *args][, **keywords])

Alias of functools.partial

fp.pp(func : callable[, *args][, **keywords]) → partial callable

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']
fp.c(f : callable, g : callable) → callable

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']
fp.const(x) → callable

Returns a function which always returns x regardless of arguments

>>> const('foo')(1, 2, foo='bar')
'foo'
fp.callreturn(method : callable, obj : a, *args, **kwargs) → a

Calls the method

>>> from six.moves import reduce
>>> reduce(
...    p(callreturn, set.add),
...    ["a", "b", "c"],
...    set()
... ) == set(["a", "b", "c"])
True
fp.kwfunc(func[, keys=None : None | list]) → callable

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
fp.identity(x) → x

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.

fp.itake(n, iterable)

Takes n items off the iterable:

>>> list(itake(3, range(5)))
[0, 1, 2]
>>> list(itake(3, []))
[]
fp.idrop(n, iterable)

Drops the first n items off the iterator

>>> list(idrop(3, range(6)))
[3, 4, 5]
>>> list(idrop(3, []))
[]
fp.isplitat(i, iterable)

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]]
fp.izipwith(f, iterable1, iterable2)

Zips a function with two iterables

>>> list(izipwith(lambda x,y: (x,y), [1,2], [3,4]))
[(1, 3), (2, 4)]

Reducers

fp.allmap(f, iterable)

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
fp.anymap(f, iterable)

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

fp.even(x)

True if x is even

>>> even(1)
False
>>> even(2)
True
fp.odd(x)

True if x is even

>>> odd(1)
True
>>> odd(2)
False

Project Versions

Previous topic

Welcome to fp’s documentation!

Next topic

fp.monads — A collection of monads

This Page