functools: Add very simple implementation of reduce()

The implementation was taken from
https://docs.python.org/3/library/functools.html
pull/59/merge
Michael Buesch 2015-12-05 20:25:25 +01:00 zatwierdzone przez Paul Sokolovsky
rodzic f9eed2340d
commit 18c9084a27
4 zmienionych plików z 19 dodań i 2 usunięć

Wyświetl plik

@ -14,3 +14,13 @@ def update_wrapper(wrapper, wrapped):
def wraps(wrapped):
# Dummy impl
return lambda x: x
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value

Wyświetl plik

@ -1,3 +1,3 @@
srctype = micropython-lib
type = module
version = 0.0.3
version = 0.0.4

Wyświetl plik

@ -6,7 +6,7 @@ from setuptools import setup
setup(name='micropython-functools',
version='0.0.3',
version='0.0.4',
description='functools module for MicroPython',
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
url='https://github.com/micropython/micropython/issues/405',

Wyświetl plik

@ -0,0 +1,7 @@
from functools import reduce
res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
assert(res == 1 + 2 + 3 + 4 + 5)
res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 10)
assert(res == 10 + 1 + 2 + 3 + 4 + 5)