transducer.transducers

Functions for creating transducers.

The functions in this module return transducers.

Blah blah blah

Transducer Factories

Call these functions to obtain transducer objects.

mapping Create a mapping transducer with the given transform.
mapping(transform)

Create a mapping transducer with the given transform.

Parameters:transform – A single-argument function which will be applied to each input element to produce the corresponding output element.
Returns:A mapping transducer: A single argument function which, when passed a reducing function, returns a new reducing function which applies the specified mapping transform function before delegating to the original reducer.

Examples

Mapping a squaring function over a list:

>>> from transducer.eager import transduce
>>> from transducer.reducers import appending
>>> from transducer.transducers import mapping
>>> m = mapping(lambda x: x*x)
>>> a = [1, 7, 9, 4, 3, 2]
>>> transduce(m, appending(), a)
[1, 49, 9, 4, 3, 2]
filtering(predicate)

Create a filtering transducer with the given predicate.

Parameters:predicate – A single-argument function which will be used to test each element and which must return True or False. Only those elements for which this function returns True will be retained.
Returns:A filtering transducer: A single argument function which, when passed a reducing function, returns a new reducing function which passes only those items in the input stream which match satisfy the predicate to the original reducer.

Examples

Filtering even numbers from a list:

>>> from transducer.eager import transduce
>>> from transducer.reducers import appending
>>> from transducer.transducers import filtering
>>> f = filtering(lambda x: x % 2 == 0)
>>> a = [1, 7, 9, 4, 3, 2]
>>> transduce(f, appending(), a)
[4, 2]
reducing(reducer, init=UNSET)

Create a reducing transducer with the given reducer.

Parameters:reducer – A two-argument function which will be used to combine the partial cumulative result in the first argument with the next item from the input stream in the second argument.
Returns:A reducing transducer: A single argument function which, when passed a reducing function, returns a new reducing function which entirely reduces the input stream using ‘reducer’ before passing the result to the reducing function passed to the transducer.

Examples

Reducing a list of numbers to a single value by summing them:

>>> from transducer.eager import transduce
>>> from transducer.reducers import expecting_single
>>> from transducer.transducers import reducing
>>> r = reducing(lambda x, y: x + y)
>>> a = [1, 7, 9, 4, 3, 2]
>>> transduce(r, expecting_single(), a)
>>> 26