コード例 #1
0
ファイル: pi.py プロジェクト: zomux/stream.py
def Gregory(type=float):
    """Return partial sums of the Gregory series converging to atan(1) == pi/4.

	Yield 1 - 1/3 + 1/5 - 1/7 + ... computed with the given type.
	"""
    return seq(type(1), step=2) >> map(lambda x: 1 / x) >> alt_sign >> fold(
        operator.add)
コード例 #2
0
ファイル: split_xform.py プロジェクト: jpfairbanks/streaming
    odd_ans  = reduce(ops.add, odds )
    static_time = time() - ts

    #streaming computation

    # create our filters
    cong_2 = lambda x: x%2==0
    evens = filter(cong_2)
    odds  = filter(lambda x: not cong_2(x))
    ts = time()
    # wire the split into the filters
    instream >> tee(evens)
    instream >> odds

    # wire up the map and fold (scan/accumulate)
    foldedevens = (evens >> stream.map(math.sqrt) >> fold(ops.add))
    print(time() - ts)
    sqrtodds = odds >> (stream.Processor(my_sqrt))
    print("established the sqrter %f" % (time() - ts))
    foldedodd = sqrtodds >> stream.fold(ops.add)
    print("made odd folder: %f" % (time() - ts))
    # force execution
    soans = foldedodd >> item[-1:]
    print(soans)
    print(time() - ts)
    seans = foldedevens >> item[:]
    print(time() - ts)
    stream_time = time() - ts

    #print(even_ans)
    #print(seans >> item[:])
コード例 #3
0
ファイル: split_scan.py プロジェクト: jpfairbanks/streaming
    evens = instream[::2]
    odds = instream[1::2]
    even_ans = scan(ops.add, evens)
    odd_ans = scan(ops.add, odds )
    static_time = time() - ts

    #streaming
    #these just create objects that are going to accept streams
    cong_2 = lambda x: x%2==0
    evens = filter(cong_2)
    odds  = filter(lambda x: not cong_2(x))
    ts = time()
    #do the wiring
    instream >> tee(evens)
    instream >> odds
    seans = evens >> fold(ops.add)
    soans = odds  >> fold(ops.add)
    #seans = evens >> stream.reduce(ops.add)
    #soans = odds  >> stream.reduce(ops.add)

    # trigger execution
    seans = seans >> item[:]
    soans = soans >> item[:]
    stream_time = time() - ts
    #print(even_ans)
    #print(seans >> item[:])
    #print(odd_ans)
    #print(soans >> item[:])


    #make sure we got the right answer
コード例 #4
0
ファイル: randwalk.py プロジェクト: Answeror/stream.py
#!/usr/bin/env python

import operator

from random import choice
from stream import repeatcall, apply, fold, takei, takewhile, drop, item


vectoradd = lambda u, v: zip(u, v) >> apply(operator.add) >> list

Origin = [0, 0]
Directions = [[1,0], [0,1], [-1,0], [0,-1]]

randwalk = lambda: repeatcall(choice, Directions) >> fold(vectoradd, initval=Origin)

def returned(n):
	"""Generate a random walk and return True if the walker has returned to
	the origin after taking `n` steps.
	"""
	## `takei` yield lazily so we can short-circuit and avoid computing the rest of the walk
	for pos in randwalk() >> drop(1) >> takei(xrange(n-1)):
		if pos == Origin:
			return True
	return False

def first_return():
	"""Generate a random walk and return its length upto the moment
	that the walker first returns to the origin.

	It is mathematically provable that the walker will eventually return,
	meaning that the function call will halt, although it may take
コード例 #5
0
ファイル: pi.py プロジェクト: Answeror/stream.py
def Gregory(type=float):
	"""Return partial sums of the Gregory series converging to atan(1) == pi/4.

	Yield 1 - 1/3 + 1/5 - 1/7 + ... computed with the given type.
	"""
	return seq(type(1), step=2) >> map(lambda x: 1/x) >> alt_sign >> fold(operator.add)