diff --git a/functools/functools.py b/functools/functools.py index 070be94c..ae8c2cf0 100644 --- a/functools/functools.py +++ b/functools/functools.py @@ -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 diff --git a/functools/metadata.txt b/functools/metadata.txt index 027b13e5..65d7b523 100644 --- a/functools/metadata.txt +++ b/functools/metadata.txt @@ -1,3 +1,3 @@ srctype = micropython-lib type = module -version = 0.0.3 +version = 0.0.4 diff --git a/functools/setup.py b/functools/setup.py index 1a000ea3..97945d50 100644 --- a/functools/setup.py +++ b/functools/setup.py @@ -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', diff --git a/functools/test_reduce.py b/functools/test_reduce.py new file mode 100644 index 00000000..30f38eec --- /dev/null +++ b/functools/test_reduce.py @@ -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)