コード例 #1
0
ファイル: demo.py プロジェクト: yarray/frpy
def compl3():
    clk, tick = clock()
    sp = fmap(lambda _: random.random(), repeat(0.2, clk))
    events = merge([clk, sp], ['clock', 'value'])

    def update(state: Tuple[float, float], event) -> Tuple[float, float]:
        channel, data = event
        start_at, acc = state
        if channel == 'clock':
            if data - start_at > time_thres:
                print('failed')
                return (data, 0)
            return state
        if channel == 'value':
            new_value = acc + data
            print(new_value)
            if new_value >= value_thres:
                print('met')
                return (time.time(), 0)
            return (start_at, new_value)
        else:
            return state

    scan(update, (time.time(), 0), events)
    tick()
コード例 #2
0
ファイル: demo.py プロジェクト: yarray/frpy
def compl2():
    # compl using async transformation
    clk, tick = clock()
    sp = fmap(soft(random.random), repeat(0.2, clk))

    # aysnc generator transformation
    async def fn(s):
        acc = 0
        last = math.inf
        async for topic, v in s:
            if topic == 'clock':
                if acc > value_thres:
                    met = True
                if v - last > time_thres or acc > value_thres:
                    yield 'met' if met else 'fail'
                    yield 0
                    met = False
                    acc = 0
                    last = v

            elif topic == 'value':
                acc += v
                yield acc

    # map the transformation over async generators to that over streams
    res = fmap_async(fn, merge([clk, sp], ['clock', 'value']))

    # hook to print trace
    res.hook = print
    tick()
コード例 #3
0
def test_fmap():
    src = Stream(None)
    s = record(fmap(lambda x: x + 3, src))
    src(5)
    src(208)
    src(176)
    src(1021)
    assert s.footprint == [8, 211, 179, 1024]
コード例 #4
0
ファイル: demo.py プロジェクト: yarray/frpy
def compl():
    # init the clock
    clk, tick = clock()

    # construct streams
    sp = fmap(soft(random.random), repeat(0.2, clk))
    term = Stream(clk)
    interrupt = timeout(time_thres, term, term)
    value = merge([sp, fmap(const(-1), term)])
    acc = scan(lambda acc, v: acc + v if v >= 0 else 0, 0, value)
    met = changed(lambda _, y: y <= value_thres, acc)
    each(term, merge([met, interrupt]))

    # hook to print trace
    acc.hook = print
    met.hook = bind(print, 'met!')
    interrupt.hook = bind(print, 'fail!')

    # start clock
    tick()
コード例 #5
0
ファイル: demo.py プロジェクト: yarray/frpy
def main():
    clk, tick = clock()
    sp = sequence(1, iter(range(1, 1000)), clk)
    sn = sequence(2, iter(range(-1, -1000, -1)), clk)
    sns = scan(lambda acc, x: acc + x, 0, sn)
    spu = changed(lambda x, y: x == y, sp)
    spuf = where(lambda x: x % 3 != 0, spu)
    sm = merge([spuf, sns])
    ss = trace(lambda x: x % 2, 9999, sm)
    sst = flatten(fmap(lambda s: diff(lambda x, y: y - x, 0, s), ss))
    sst.hook = print
    sp(2)
    sp(1)
    sp(1)

    sstop = fmap(lambda _: 'stop', repeat(5, clk))
    sto = timeout(1.5, sstop, sn)
    events = merge([sn, sstop, sto])
    events.hook = print

    tick()
コード例 #6
0
ファイル: demo.py プロジェクト: yarray/frpy
def amain():
    clk, tick = clock()
    counter1 = iter(range(1, 1000))
    sp = fmap(lambda _: next(counter1), repeat(1, clk))

    async def mult2(s):
        async for e in s:
            if e % 5 != 0:
                yield e * 2

    sp2 = fmap_async(mult2, sp)
    sp2.hook = print
    tick()
コード例 #7
0
ファイル: demo.py プロジェクト: yarray/frpy
def action_q():
    clk, tick = clock()
    events = Stream(clk)

    def update(state: float, event) -> float:
        channel, data = event
        acc = state
        if channel == 'reset':
            return 0
        else:
            acc += data
            if acc >= 3:
                events(('reset', None))
            return acc

    sp = fmap(const(('value', 1)), repeat(0.4, clk))
    each(events, sp)
    states = scan(update, 0, events)
    states.hook = print
    tick()
コード例 #8
0
ファイル: demo.py プロジェクト: yarray/frpy
def timing():
    clk, tick = clock()
    a = fmap(lambda _: 1, repeat(0.3, clk))
    b = fmap(lambda _: 22222222, a)
    each(print, merge([a, b]))
    tick()