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

fbox(*args)

Given any number of args, box them into a tuple.

Aliases: fboxed

Parameters:*args – any number of objects to box as a tuple
Returns:a tuple containing the arguments that were boxed
funbox(callable, *initial_args, **initial_kwargs)(*boxed_args, **boxed_kwargs)

Given a callable, return a closure that when called with arguments will expand (unbox) them before applying them to the callable.

Parameters:
  • callable (function) – the function whose closure will call
  • *initial_args – any default arguments to initially pass to the callable
  • **initial_kwargs – any default keyword arguments to initially pass to the callable
  • *boxed_args – any number of arguments which are unboxed and concatenated together
  • **boxed_kwargs – any extra keyword arguments to apply to the callable
finstance(type)(object)

Given a type, return a closure that will return true or false depending on whether object is an instance of that type.

Parameters:
  • type (type) – any kind of python type
  • object – any kind of python object to test
fhasitem(item)(object)

Given an item, return a closure that will return true or false based on whether or not object contains it.

Aliases: fitemQ

Parameters:
  • item – any kind of python object
  • object – any kind of python object to test membership with
fgetitem(item, *default)(object)

Given an item, return a closure which fetches item from object. If default is specified, then if the item does not exist in object return it instead.

Aliases: fitem

Parameters:
  • item – any kind of python object to pass to operator.getitem().
  • object – any kind of python object to return an item from
  • *default – an item returned by default if the object does not contain the specified item
fhasattr(attribute)(object)

Given an attribute as a string, return a closure that will return true or false based on whether or not object has the specified attribute.

Aliases: fattributeQ

Parameters:
  • attribute (str) – the attribute to check for
  • object – any kind of python object to test
fgetattr(attribute, *default)(object)

Given an attribute, return a closure which fetches the attribute from the object. If default is specified, then if the attribute does not exist in object 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 specified attribute
fpassthru(object)

Given an object, return it. This is the identity function and is typically used to ignore transforming an object.

Aliases: fpass, fidentity, fid

Parameters:object – any kind of python object to return
fdefault(default)(object)

Given a default object, return a closure that will return it if object is not defined (false-y).

Parameters:
  • default – the default object to return
  • object – any kind of python object to check
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)(*args, **kwargs)

Given a callable, return a closure that will call it with no parameters whilst discarding any that were passed to it.

Parameters:
  • callable – a callable to execute
  • *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 for true and false. This will return another closure that when passed an object, will check it via the critiquing function (crit) and return true if the function returns a truthy value, or return false if it returns a false-y value.

Aliases: fcond

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 against object returning a tuple containing the result of each callable.

Parameters:
  • *callables – any number of callables to execute for each desired result returned
  • object – any kind of python object to use
fmemo(callable, *initial_args, **initial_kwargs)(*args, **kwargs)

Given a callable, and any initial_args and initial_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.

Aliases: flazy

Parameters:
  • callable (function) – any callable to memoize the results 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 both start_args and start_kwargs. This will return a closure that can then be called with any other args or keyword arguments in kwargs.

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 and default_kwargs, return a closure that will apply these arguments to its first parameter callable. If args or kwargs 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
frpartial(callable, *reverse_args, **reverse_kwargs)(*args, **kwargs)

Given a callable, the arguments reverse_args, and the keyword arguments reverse_kwargs, return a closure that will apply these to the callable backwards. If args or kwargs is provided, then apply these to the front of the callable.

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
freversed(callable, *reverse_args, **reverse_kwargs)(*extra_args, **extra_kwargs)

Given a callable, the arguments reverse_args, and the keyword arguments reverse_kwargs, return a closure which applies these to the end of the callable. If extra_args or extra_kwargs is provided, then continue to apply these to the callable but backwards.

Aliases: freverse

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 arguments initial_args combined with args, and the keyword arguments initial_kwargs combined with kwargs.

This closure will wrap the result of callable so that the second element of the tuple will be the result, and the first element will be the exception object if one was raised. If one wasn’t raised, then the first element will be the value None.

Aliases: fexc, fexception

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 arguments initial_args, and the keyword arguments initial_kwargs, return a closure that will invert the result (not) returned from the callable.

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 python object, return its first element.

Parameters:listable (list or tuple) – any kind of list-like object
second(iterable)

Given a listable python object, return its second element.

Parameters:listable (list or tuple) – any kind of list-like object
third(iterable)

Given a listable python object, return the third element.

Parameters:listable (list or tuple) – any kind of list-like object
last(iterable)

Given a listable python object, return its last element.

Parameters:listable (list or tuple) – any kind of list-like object
ilist(iterable)

Given a iterable python object, return it as a list.

Parameters:iterable – any kind of iterable object
liter(listable)

Given a listable python object, return it as an iterable..

Parameters:listable (list or tuple) – any kind of list-like object
ituple(iterable)

Given a iterable python object, return it as a tuple.

Parameters:iterable – any kind of iterable object
titer(tuple)

Given a tuple, return it as an iterator.

Parameters:tuple (tuple) – any kind of python tuple
itake(count)(iterable)

Given an integer count, return a closure that will consume that number of elements from the provided iterable and return them as a tuple.

Parameters:
  • count (int or long) – a number of elements to consume
  • iterable – an iterable to consume
iget(count)(iterable)

Given an integer count, return a closure that will consume that number of elements from the provided iterable and return the last one.

Parameters:
  • count (int or long) – a number of elements to consume
  • iterable – an iterable to consume values from
imap(callable, iterable)

Execute the provided callable against all of the elements in iterable returning an iterator containing the transformed results. This is similar to map() but for iterables.

Parameters:
  • callable (function) – a callable python object that transforms its argument
  • iterable – an iterable to transform results from
ifilter(crit, iterable)

Yield each value from iterable that the callable crit returns true for. This is similar to filter() but for iterables.

Parameters:
  • crit (function) – a callable python object that returns true or false based on its argument
  • iterable – an iterable to critique
ichain(*iterables)

Given a variable number of iterables, combine them all into a single iterator. This is the same as itertools.chain().

Parameters:*iterables – any number of iterators
izip(*iterables)

Given any number of iterables, return them as an iterator that yields a tuple for each element that an individual iterator would return. This is similar to zip(), and is the same as itertools.izip().

Parameters:*iterables – any number of iterators
count(iterable)

Given an iterable, return the number of elements 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 using itertools.tee().

Parameters:iterable – an iterator to count the elements of