@@ -0,0 +1,1423 @@
+#!/usr/bin/env python2.6%0A%0Aimport operator%0A%0Afrom decimal import Decimal%0Afrom stream import Stream, Filter, seq, gseq, apply, map, fold, zipwith, item%0A%0A%22%22%22%0ACompute digits of pi using the Gregory series, and its accelerated variants.%0A%0AFor a detailed explanation, see this section of SICP:%0A%3Chttp://mitpress.mit.edu/sicp/full-text/sicp/book/node72.html%3E%0A%22%22%22%0A%0Aalt_sign = lambda s: s %3E%3E zipwith(gseq(1, -1)) %3E%3E apply(operator.mul)%0A%0A## Return the partial sums of the Gregory series converging to atan(1) == pi/4%0A## @param t: the type of number we want to work with: either float or Decimal%0Adef Gregory(t=float):%0A%09return seq(t(1), step=2) %3E%3E map(lambda x: 1/x) %3E%3E alt_sign %3E%3E fold(operator.add)%0A%0Aseries1 = Gregory()%0A%0A## Accelerate the convergence by transforming the series%0A## using Aitken's delta-squared process (SCIP calls it Euler)%0A@Filter%0Adef Aitken(inpipe):%0A%09def genfunc():%0A%09%09s0, s1, s2 = inpipe %3E%3E item%5B:3%5D%0A%09%09while 1:%0A%09%09%09yield s2 - (s2 - s1)**2 / (s0 - 2*s1 + s2)%0A%09%09%09s0, s1, s2 = s1, s2, next(inpipe)%0A%09return genfunc()%0A%0Aseries2 = Gregory() %3E%3E Aitken%0A%0A## Recursively apply a transformation%0Aclass recur_transform(Stream):%0A%09def __init__(self, accelerator):%0A%09%09super(recur_transform, self).__init__()%0A%09%09self.accelerator = accelerator%0A%09%0A%09def __call__(self, inpipe):%0A%09%09def genfunc():%0A%09%09%09s = inpipe%0A%09%09%09while 1:%0A%09%09%09%09yield next(s)%0A%09%09%09%09s = s %3E%3E self.accelerator%0A%09%09return genfunc()%0A%0Aseries3 = Gregory(Decimal) %3E%3E recur_transform(Aitken)%0A

Add example/pi.py