kopia lustrzana https://github.com/micropython/micropython-lib
functools: Add very simple implementation of reduce()
The implementation was taken from https://docs.python.org/3/library/functools.htmlpull/59/merge
rodzic
f9eed2340d
commit
18c9084a27
|
@ -14,3 +14,13 @@ def update_wrapper(wrapper, wrapped):
|
||||||
def wraps(wrapped):
|
def wraps(wrapped):
|
||||||
# Dummy impl
|
# Dummy impl
|
||||||
return lambda x: x
|
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
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
srctype = micropython-lib
|
srctype = micropython-lib
|
||||||
type = module
|
type = module
|
||||||
version = 0.0.3
|
version = 0.0.4
|
||||||
|
|
|
@ -6,7 +6,7 @@ from setuptools import setup
|
||||||
|
|
||||||
|
|
||||||
setup(name='micropython-functools',
|
setup(name='micropython-functools',
|
||||||
version='0.0.3',
|
version='0.0.4',
|
||||||
description='functools module for MicroPython',
|
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.",
|
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',
|
url='https://github.com/micropython/micropython/issues/405',
|
||||||
|
|
|
@ -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)
|
Ładowanie…
Reference in New Issue