Skip to content

pfun.functions.identity(v)

The identity function. Just gives back its argument

Examples:

>>> identity('value')
'value'

Parameters:

Name Type Description Default
v ~A

The value to get back

required

Returns:

Type Description
~A

v

pfun.functions.always(value)

Get a function that always returns value

Examples:

>>> f = always(1)
>>> f(None)
1
>>> f('')
1
>>> "... and so on..."

Parameters:

Name Type Description Default
value ~A

The value to return always

required

Returns:

Type Description
Callable[..., ~A]

function that always returns value

pfun.functions.compose(f, g, *functions)

Compose functions from left to right

Examples:

>>> f = lambda v: v * 2
>>> g = compose(str, f)
>>> g(3)
"6"

Parameters:

Name Type Description Default
f Callable[[Any], Any]

the outermost function in the composition

required
g Callable[[Any], Any]

the function to be composed with f

required
functions Callable[[Any], Any]

functions to be composed with f and g from left to right

()

Returns:

Type Description
Callable[[Any], Any]

f composed with g composed with functions from left to right

pfun.functions.pipeline(first, second, *rest)

Compose functions from right to left

Examples:

>>> f = lambda v: v * 2
>>> g = pipeline(f, str)
>>> g(3)
"6"

Parameters:

Name Type Description Default
first Callable[[Any], Any]

the innermost function in the composition

required
second Callable[[Any], Any]

The second-innermost function in the composition

required
rest Callable[[Any], Any]

functions to compose with first and second from right to left

()

Returns:

Type Description
Callable[[Any], Any]

rest composed from right to left, composed with second composed with first

pfun.functions.curry(f)

Get a version of f that can be partially applied

Examples:

>>> f = lambda a, b: a + b
>>> f_curried = curry(f)
>>> f_curried(1)
functools.partial(<function <lambda> at 0x1051f0950>, a=1)
>>> f_curried(1)(1)
2

Parameters:

Name Type Description Default
f Callable

The function to curry

required

Returns:

Type Description
Callable

Curried version of f