2.4. Functional combinators¶
To simplify the generation of functions that can contain potentially complex logic, a number of primitive functions are provided which can be combined in arbitrary ways. This can facilitate the quick creation of functions that can be used as one-liners.
This was primarily developed for Matching/Filtering of IDA types, but is also exposed to the user if they wish to use it in their endeavors. These can be used for creating one-liner functions that transform an input into various types, constructing a function that will consume or test its input, or really just any number of things.
2.4.1. Examples¶
To create a closure that will return the type of the second element of a structure:
> f = fcompose( fgetitem(1), fattribute('type') )
>
> st = struc.by('MyStructName')
> print f(st)
To create a closure which will try and call __repr__()
whilst
falling back to formatting as a string if not possible:
> test = fcondition(fhasattr('__repr__'))
> tf = test(fgetattr('__repr__'), "{!s}".format)
> f = fcompose(tf, fcurry())
> print f(object)
To create a closure which will return a string when defined and an empty string when undefined:
> f = fcompose(fdefault(''), "{!s}".format)
> print f('hi')
> print f(None)
2.4.2. Combinators¶
-
fpack
(callable, *initial_args, **initial_kwargs))(*packed_args, **packed_kwargs)¶ Given a
callable
, return a closure that when called with arguments will pack (box) them before applying them to thecallable
as its first parameter. Any keyword arguments will be merged together when executing thecallable
.Parameters: - callable (function) – the function which will be called by the returned closure
- *initial_args – any default arguments to prefix the parameter that is passed to the
callable
with - **initial_kwargs – any default keyword arguments to pass to the
callable
- *packed_args – any number of arguments which are packed and then concatenated together
- **packed_kwargs – any extra keyword arguments to apply to the callable
-
funpack
(callable, *initial_args, **initial_kwargs)(*packed_args, **packed_kwargs)¶ Given a
callable
, return a closure that when called with arguments will unpack (unbox) them before applying them to thecallable
as its parameters. Any keyword arguments will be merged together when executing thecallable
.Parameters: - callable (function) – the function which will be called by the returned closure
- *initial_args – any default arguments to initially pass to the
callable
- **initial_kwargs – any default keyword arguments to initially pass to the
callable
- *packed_args – any number of arguments which are unpacked and then concatenated together
- **packed_kwargs – any extra keyword arguments to apply to the callable
-
fcar
(callable, *initial_args, **initial_kwargs))(*args, **kwargs)¶ Given a
callable
, return a closure that when called with arguments will only use the first element fromargs
parameter appended to the providedinitial_args
. Any keyword arguments will be merged together when executing thecallable
. This is similar to thecar
function found in programming languages from the LISP family.Parameters: - callable (function) – the function which will be called by the returned closure
- *initial_args – any default arguments to prefix the parameter that is passed to the
callable
with - **initial_kwargs – any default keyword arguments to pass to the
callable
- *args – any number of arguments from which to use only the first one
- **kwargs – any extra keyword arguments to apply to the callable
-
fcdr
(callable, *initial_args, **initial_kwargs))(*args, **kwargs)¶ Given a
callable
, return a closure that when called with arguments will use all except the first element fromargs
parameter and append it to the end of the providedinitial_args
. Any keyword arguments will be merged together when executing thecallable
. This is similar to thecdr
function found in programming languages from the LISP family.Parameters: - callable (function) – the function which will be called by the returned closure
- *initial_args – any default arguments to prefix the parameter that is passed to the
callable
with - **initial_kwargs – any default keyword arguments to pass to the
callable
- *args – any number of arguments from which to use only the first one
- **kwargs – any extra keyword arguments to apply to the callable
-
finstance
(type)(object)¶ Given a
type
, return a closure that will return either :py:const`True` orFalse
based on whetherobject
is an instance of the providedtype
.Parameters: - type (type) – any kind of python type
- object – any kind of python object to test
Returns: whether the object is an instance of the requested type or not
Return type: bool
-
fhasitem
(item)(object)¶ Given an
item
, return a closure that will return true or false based on whether or notobject
contains it via the “contains” operator.Aliases:
fitemQ
Parameters: - item – any kind of python object
- object – any kind of python object to test membership with
Returns: whether the object has the requested item or not
Return type: bool
-
fgetitem
(item, *default)(object)¶ Given an
item
, return a closure which fetches item fromobject
. Ifdefault
is specified, then if the item does not exist inobject
return it instead.Aliases:
fitem
Parameters: - item – any kind of python object to pass as the key to the
operator.getitem()
function - object – any kind of python object to return an item from
- *default – an item returned by default if the
object
does not contain the specifieditem
Returns: the item that was requested
- item – any kind of python object to pass as the key to the
-
fsetitem
(item)(value)(object)¶ Assign the given
value
to the specifieditem
of the providedobject
when called and then return the modifiedobject
.Parameters: - item – any kind of python object to pass as the key to the
operator.setitem()
function - value – the value to assign to the python object as used by
operator.setitem()
- object – any kind of python object to assign the item into
Returns: the object that was modified
- item – any kind of python object to pass as the key to the
-
fdelitem
(*items)(object)¶ Return a closure that when called with a particular
object
, will remove the designateditems
from it prior to returning the modifiedobject
.Parameters: - *items – any number of python objects to pass as keys to the
operator.delitem()
function - object – any kind of python object to remove the items from
Returns: the object that was modified
- *items – any number of python objects to pass as keys to the
-
fhasattr
(attribute)(object)¶ Given an
attribute
as a string, return a closure that will returnTrue
orFalse
based on whether or notobject
has the specifiedattribute
.Aliases:
fattributeQ
Parameters: - attribute (str) – the attribute to check for
- object – any kind of python object to test
Returns: whether the object has the requested attribute or not
Return type: bool
-
fgetattr
(attribute, *default)(object)¶ Given an
attribute
, return a closure which fetches the attribute from theobject
. Ifdefault
is specified, then if the attribute does not exist inobject
return it instead.Aliases:
fattribute
Parameters: - attribute (str) – an attribute to return from the
object
- object – any kind of python object to return an attribute from
- *default – an attribute returned by default if the
object
does not contain with specifiedattribute
Returns: the requested attribute
- attribute (str) – an attribute to return from the
-
fsetattr
(attribute)(value)(object)¶ Assign the given
value
to the specifiedattribute
of the providedobject
when called and return theobject
.Aliases:
fsetattribute
Parameters: - attribute – an attribute on the
object
to assign. - value – the value to assign to the python object as used by
builtins.setattr()
. - object – any kind of python object to assign the attribute on.
Returns: the object that was modified
- attribute – an attribute on the
-
fconstant
(object)¶ Return a closure that always returns the provided
object
.Aliases:
fconst
,falways
Parameters: object – any kind of python object to return Returns: a closure that returns the object
-
fidentity
(object)¶ Given an
object
, return it. This is the identity function and is typically used to ignore transforming an object.Parameters: object – any kind of python object to return Returns: the object
-
fdefault
(default)(object)¶ Given a
default
object, return a closure that will return it ifobject
is not defined (false-y).Parameters: - default – the default object to return
- object – any kind of python object to check
Returns: the object if it is defined, otherwise the value for default
-
fcompose
(*callables)(object)¶ Given a number of
callables
, return a closure that executes them in succession whilst returning the result.Parameters: - *callables – a number of callables that each take one parameter
- object – any kind of python object to transform
-
fdiscard
(callable, *initial_args, **initial_kwargs)(*args, **kwargs)¶ Given a
callable
and anyinitial_args
orinitial_kwargs
, return a closure that will call it with only those parameters whilst discarding any parameters that were passed to the returned closure.Parameters: - callable (function) – a callable to execute
- *initial_args – any default arguments to pass as parameters to the
callable
- **initial_kwargs – any default keyword arguments to pass as parameters to the
callable
- *args – any number of arguments that get discarded
- **kwargs – any kind of keyword arguments that get discarded
-
fcondition
(crit)(true, false)(object)¶ Given a critiquing function
crit
, return a closure which takes parameters fortrue
andfalse
. This will return another closure that when passed anobject
, will check it via the critiquing function (crit
) and returntrue
if the function returns a truthy value, or returnfalse
if it returns a false-y value.Parameters: - crit (function) – a callable that takes an argument and returns true or false
- true (object or function) – an object or a function to return (or execute) when value is true
- false (object or function) – an object or a function to return (or execute) when value is false
- object – any kind of python object to pass to
crit
-
fmap
(*callables)(object)¶ Given a number of
callables
, return a closure that executes them synchronously againstobject
returning a tuple composed of the results of each callable.Parameters: - *callables – any number of callables to execute for each desired result returned
- object – any kind of python object to use
-
flazy
(callable, *initial_args, **initial_kwargs)(*args, **kwargs)¶ Given a
callable
, and anyinitial_args
andinitial_kwargs
, return a closure that caches (memoizes) the result that is returned. The next time this closure is called with the same arguments, the cached version will be returned instead.Parameters: - callable (function) – any callable to execute lazily and memoize its result for
- *initial_args – any initial arguments to prefix to the callable
- **initial_kwargs – any initial keyword arguments to apply to the callable
- *args – any arguments to apply to the callable
- **kwargs – any keyword arguments to apply to the callable
-
fpartial
(callable, *start_args, **start_kwargs)(*args, **kwargs)¶ Given a
callable
, partially apply the arguments specified in bothstart_args
andstart_kwargs
. This will return a closure that can then be called with any otherargs
or keyword arguments inkwargs
.Parameters: - callable (function) – any callable to partially apply arguments to
- *start_args – initial arguments to partially apply to the
callable
- **start_kwargs – initial keyword arguments to partially apply to the
callable
- *args – arguments to continue to apply to the callable
- **kwargs – any keyword arguments to continue to apply to the callable
-
fapply
(callable, *initial_args, **initial_kwargs)(*args, **kwargs)¶ Given a
callable
, return a closure that will apply both the arguments (args
) and keyword arguments (kwargs
) to it.Parameters: - callable (function) – any callable to apply arguments to
- *args – the arguments to apply to the
callable
- **kwargs – the keyword arguments to apply to the
callable
- *initial_args – any initial arguments to prefix the
args
with - **initial_kwargs – any initial keyword args to prefix the
kwargs
with
-
fcurry
(*default_args, **default_kwargs)(callable, *args, **kwargs)¶ Given
default_args
anddefault_kwargs
, return a closure that will apply these arguments to its first parametercallable
. Ifargs
orkwargs
is specified, the append these to the default arguments.Parameters: - *default_args – the arguments to apply to the
callable
- **default_kwargs – the keyword arguments to apply to the
callable
- callable (function) – the callable to apply the arguments to
- *args – any extra arguments to apply to the
callable
- **kwargs – any extra keyword arguments to apply to the
callable
- *default_args – the arguments to apply to the
-
frpartial
(callable, *reverse_args, **reverse_kwargs)(*args, **kwargs)¶ Given a
callable
, the argumentsreverse_args
, and the keyword argumentsreverse_kwargs
, return a closure that will apply these to thecallable
in reverse. Ifargs
orkwargs
is provided, then apply these to the front of thecallable
.Parameters: - callable (function) – the callable to apply the arguments to
- *reverse_args – the arguments to apply to the end of the
callable
- **reverse_kwargs – the keyword arguments to apply to the
callable
- *args – the arguments to apply to the beginning of the
callable
- **kwargs – any extra keyword arguments to apply to the
callable
-
freverse
(callable, *reverse_args, **reverse_kwargs)(*extra_args, **extra_kwargs)¶ Given a
callable
, the argumentsreverse_args
, and the keyword argumentsreverse_kwargs
, return a closure which applies these to the end of thecallable
. Ifextra_args
orextra_kwargs
is provided, then continue to apply these to thecallable
but in reverse.Parameters: - callable (function) – the callable to apply the arguments to
- *reverse_args – the arguments to apply to the end of
callable
- **reverse_kwargs – the keyword arguments to apply to
callable
- *extra_args – extra arguments to continue to apply to
kwargs
- **extra_kwargs – any extra keyword arguments to apply to
callable
-
fcatch
(callable, *initial_args, **initial_kwargs)(*args, **kwargs)¶ Given a
callable
, return a closure that will call it with the argumentsinitial_args
combined withargs
, and the keyword argumentsinitial_kwargs
combined withkwargs
.This closure will wrap the result of
callable
so that the second element of the returned tuple will be the result, and the first element will be the exception object if one was raised. If an exception was not raised, then the first element will be the valueNone
, and the second will be the original result.Parameters: - callable (function) – the callable to catch an exception in
- *initial_args – the initial arguments to apply to the
callable
- **initial_kwargs – the initial keyword arguments to apply to the
callable
- *args – the arguments to apply to the
callable
- **kwargs – the keyword arguments to apply to the
callable
-
fcomplement
(callable, *initial_args, **initial_kwargs)(*args, **kwargs)¶ Given a
callable
, the argumentsinitial_args
, and the keyword argumentsinitial_kwargs
, return a closure that will invert the result (not) prior to returning it.Aliases:
fnot
Parameters: - callable (function) – the callable to invert the result for
- *initial_args – the initial arguments to apply to the
callable
- **initial_args – the initial keyword arguments to apply to the
callable
- *args – the arguments to apply to the
callable
- **kwargs – the keyword arguments to apply to the
callable
-
first
(listable)¶ Given a
listable
object, return the first element in its sequence.Parameters: listable (list or tuple) – any kind of list-like object Returns: the first element from the iterable
-
second
(iterable)¶ Given a
listable
object, return the second element in its sequence.Parameters: listable (list or tuple) – any kind of list-like object Returns: the second element from the iterable
-
third
(iterable)¶ Given a
listable
object, return the third element in its sequence.Parameters: listable (list or tuple) – any kind of list-like object Returns: the third element from the iterable
-
last
(iterable)¶ Given a
listable
object, return the last element in its sequence.Parameters: listable (list or tuple) – any kind of list-like object Returns: the last element from the iterable
-
ilist
(iterable)¶ Given an
iterable
object, return it as alist
.Parameters: iterable – any kind of iterable object Returns: the items from the iterator Return type: list
-
liter
(listable)¶ Given a
listable
object, return it as an iterable.Parameters: listable (list or tuple) – any kind of list-like object Returns: an iterator composed of the items from the list
-
ituple
(iterable)¶ Given an
iterable
object, return it as atuple
.Parameters: iterable – any kind of iterable object Returns: the items from the iterator Return type: tuple
-
titer
(tuple)¶ Given a
tuple
, return it as an iterator.Parameters: tuple (tuple) – any kind of python tuple Returns: an iterator composed of the items from the tuple
-
itake
(count)(iterable)¶ Given an integer
count
, return a closure that will consume that number of elements from the providediterable
and return them as atuple
.Parameters: - count (
int
orlong
) – a number of elements to consume - iterable – an iterable to consume
Returns: the items that were selected
Return type: tuple
- count (
-
iget
(index)(iterable)¶ Given an integer
index
, return a closure that will consume the required number of elements from the providediterable
in order to return the element at the requested index.Parameters: - count (
int
orlong
) – a number of elements to consume - iterable – an iterable to consume values from
Returns: the item at the requested index
- count (
-
islice
(iterable, stop)¶
-
islice
(iterable, start, stop[, step]) Given an
iterable
, return an iterator which yields the selected values fromstart
tostop
. Ifstep
is provided, then use its value as the number of values to skip when yielding values. This is similar to using theoperator.getitem()
function with theslice
class for iterators.Parameters: - iterable – an iterable to transform results from
- start (
int
orlong
) – the index to start at - stop (
int
orlong
) – the index to stop at - step (
int
orlong
) – the number of values to skip
Returns: the selected items as an iterator
-
imap
(callable, iterable)¶ Execute the provided
callable
against all of the elements initerable
returning an iterator containing the transformed results. This is similar tomap()
and is the same as theitertools.imap()
function from Python 2.x, or the regularmap()
function from Python 3.x.Parameters: - callable (function) – a callable python object that transforms its argument
- iterable – an iterable to transform results from
Returns: the transformed items as an iterator
-
ifilter
(crit, iterable)¶ Yield each value from
iterable
that the callablecrit
returnsTrue
for. This is similar tofilter()
and is the same as theitertools.ifilter()
function from Python 2.x, or the regularfilter()
function from Python 3.x.Parameters: - crit (function) – a callable python object that returns
True
orFalse
based on its argument - iterable – an iterable to critique
Returns: the filtered items as an iterator
- crit (function) – a callable python object that returns
-
ichain
(*iterables)¶ Given a variable number of
iterables
, concatenate all of them into a single iterator. This is the same as theitertools.chain()
. function.Parameters: *iterables – any number of iterators Returns: an iterator composed of all of the provided iterators executed in sequence
-
izip
(*iterables)¶ Given any number of
iterables
, return them as an iterator that yields a tuple for each element passed as positional arguments to the function. This is similar tozip()
, and is the same as theitertools.izip()
function from Python 2.x, or the regularzip()
function from Python 3.x.Parameters: *iterables – any number of iterators Returns: the items from each iterator zipped together
-
lslice
(iterable, stop)¶
-
lslice
(iterable, start, stop[, step]) Given an
iterable
, return alist
containing the selected values fromstart
tostop
. Ifstep
is provided, then use its value as the number of values to skip when returning values. This is similar to theoperator.getitem()
function being used on lists with theslice
class.Parameters: - iterable – an iterable to transform results from
- start (
int
orlong
) – the index to start at - stop (
int
orlong
) – the index to stop at - step (
int
orlong
) – the number of values to skip
Returns: the selected list of items
Return type: list
-
lmap
(callable, iterable)¶ Execute the provided
callable
against all of the elements initerable
returning alist
containing the transformed results. This is similar tomap()
function from Python 2.x, or using themap()
function from Python 3.x and iterating through the result as alist
.Parameters: - callable (function) – a callable python object that transforms its argument
- iterable – an iterable to transform results from
Returns: the transformed list of items
Return type: list
-
lfilter
(crit, iterable)¶ Return a
list
containing each value fromiterable
that the callablecrit
returnsTrue
for. This is similar to thefilter()
function from Python 2.x, or using thefilter()
function from Python 3.x and iterating through the result as alist
.Parameters: - crit (function) – a callable python object that returns
True
orFalse
based on its argument - iterable – an iterable to critique
Returns: the filtered list of items
Return type: list
- crit (function) – a callable python object that returns
-
lzip
(*iterables)¶ Given any number of
iterables
, return them as alist
composed oftuple
objects for each element passed as positional arguments to the function. This is similar to thezip()
function from Python 2.x, or thezip()
function from Python 3.x and iterating through the reuslt as alist
.Parameters: *iterables – any number of iterators Returns: the items from each iterator zipped together Return type: list
oftuple
-
count
(iterable)¶ Given an
iterable
, return the number of items that it contains.Note: This is done by consuming values from
iterable
which will modify its state. If the state of the iterator wishes to be retained, one can either re-create it, or make a copy of it usingitertools.tee()
.Parameters: iterable – an iterator to count the elements of Returns: the number of items that were found Return type: int
orlong