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:    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 <>`__.


            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

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

                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)