@@ -842,7 +842,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
842842 from contextlib import suppress
843843 from functools import reduce
844844 from math import comb, prod, sumprod, isqrt
845- from operator import itemgetter, getitem, mul, neg
845+ from operator import itemgetter, getitem, mul, neg, truediv
846846
847847 def take(n, iterable):
848848 "Return first n items of the iterable as a list."
@@ -853,9 +853,10 @@ and :term:`generators <generator>` which incur interpreter overhead.
853853 # prepend(1, [2, 3, 4]) → 1 2 3 4
854854 return chain([value], iterable)
855855
856- def tabulate(function, start=0):
857- "Return function(0), function(1), ..."
858- return map(function, count(start))
856+ def running_mean(iterable):
857+ "Yield the average of all values seen so far."
858+ # running_mean([8.5, 9.5, 7.5, 6.5]) -> 8.5 9.0 8.5 8.0
859+ return map(truediv, accumulate(iterable), count(1))
859860
860861 def repeatfunc(function, times=None, *args):
861862 "Repeat calls to a function with specified arguments."
@@ -1210,8 +1211,8 @@ The following recipes have a more mathematical flavor:
12101211 [(0, 'a'), (1, 'b'), (2, 'c')]
12111212
12121213
1213- >>> list (islice(tabulate( lambda x : 2 * x), 4 ))
1214- [0, 2, 4, 6 ]
1214+ >>> list (running_mean([ 8.5 , 9.5 , 7.5 , 6.5 ] ))
1215+ [8.5, 9.0, 8.5, 8.0 ]
12151216
12161217
12171218 >>> for _ in loops(5 ):
@@ -1748,6 +1749,10 @@ The following recipes have a more mathematical flavor:
17481749
17491750 # Old recipes and their tests which are guaranteed to continue to work.
17501751
1752+ def tabulate(function, start=0):
1753+ "Return function(0), function(1), ..."
1754+ return map(function, count(start))
1755+
17511756 def old_sumprod_recipe(vec1, vec2):
17521757 "Compute a sum of products."
17531758 return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
@@ -1827,6 +1832,10 @@ The following recipes have a more mathematical flavor:
18271832.. doctest ::
18281833 :hide:
18291834
1835+ >>> list (islice(tabulate(lambda x : 2 * x), 4 ))
1836+ [0, 2, 4, 6]
1837+
1838+
18301839 >>> dotproduct([1 ,2 ,3 ], [4 ,5 ,6 ])
18311840 32
18321841
0 commit comments