From 6c6c34d05a75954c07314bc4bdf3f87cb2729ddf Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 3 Mar 2014 16:20:00 +0100 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/util.py') 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 + -- cgit v1.2.3-70-g09d2