aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.py')
-rw-r--r--src/util.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/util.py b/src/util.py
index 4698533..6b43e83 100644
--- a/src/util.py
+++ b/src/util.py
@@ -77,3 +77,20 @@ def spawn_read(*command):
if out.endswith('\n'):
out = out[:-1]
return out
+
+
+def reduce(f, items):
+ '''
+ https://en.wikipedia.org/wiki/Fold_(higher-order_function)
+
+ @param f:(¿E?, ¿E?)→¿E? The function
+ @param item:itr<¿E?> The input
+ @return ¿E? The output
+ '''
+ if len(items) < 2:
+ return items
+ rc = items[0]
+ for i in range(1, len(items)):
+ rc = f(rc, items[i])
+ return rc
+