Python functools.html() Examples

The following are code examples for showing how to use functools.html(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: torchdata   Author: szymonmaszke   File: datasets.py    MIT License 5 votes vote down vote up
def reduce(self, function: typing.Callable, initializer=None):
        r"""**Reduce dataset to single element with function.**

        Works like `functools.reduce <https://docs.python.org/3/library/functools.html#functools.reduce>`__.

        **Example**::

            class Dataset(torchdata.Dataset):
                def __init__(self, max: int):
                    super().__init__() # This is necessary
                    self.range = list(range(max))

                def __getitem__(self, index):
                    return self.range[index]

                def __len__(self):
                    return len(self.range)

            summed_dataset = Dataset(10).reduce(lambda x, y: x + y) # Returns 45


        Parameters
        ----------
        function : typing.Callable
                Two argument function returning single value used to `reduce` dataset.
        initializer: typing.Any, optional
                Value with which reduction will start.

        Returns
        -------
        typing.Any
                Reduced value

        """
        if initializer is None:
            return functools.reduce(function, (item for item in self))
        return functools.reduce(function, (item for item in self), initializer)