Skip to content

Commit 18c9084

Browse files
mbueschpfalcon
authored andcommitted
functools: Add very simple implementation of reduce()
The implementation was taken from https://docs.python.org/3/library/functools.html
1 parent f9eed23 commit 18c9084

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

functools/functools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ def update_wrapper(wrapper, wrapped):
1414
def wraps(wrapped):
1515
# Dummy impl
1616
return lambda x: x
17+
18+
def reduce(function, iterable, initializer=None):
19+
it = iter(iterable)
20+
if initializer is None:
21+
value = next(it)
22+
else:
23+
value = initializer
24+
for element in it:
25+
value = function(value, element)
26+
return value

functools/metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
srctype = micropython-lib
22
type = module
3-
version = 0.0.3
3+
version = 0.0.4

functools/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
setup(name='micropython-functools',
9-
version='0.0.3',
9+
version='0.0.4',
1010
description='functools module for MicroPython',
1111
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.",
1212
url='https://github.com/micropython/micropython/issues/405',

functools/test_reduce.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from functools import reduce
2+
3+
res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
4+
assert(res == 1 + 2 + 3 + 4 + 5)
5+
6+
res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 10)
7+
assert(res == 10 + 1 + 2 + 3 + 4 + 5)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy