예제 #1
0
파일: test_loop.py 프로젝트: timqsh/returns
def test_fold_loop_reader(iterable, sequence):
    """Ensures that ``.loop`` works for readers."""
    assert Fold.loop(
        iterable,
        sequence.from_value(10),
        _sum_two,
    )(...) == sequence(...)
예제 #2
0
def run(target_agents: Set[Agent], enemy_agents: Set[Agent] = set()):
    agent_key_sources = (
        try_to_submit_source(agent).map(lambda sources: (agent.key, sources))
        for agent in chain(target_agents, enemy_agents))
    source_map = (Fold.loop(
        agent_key_sources,
        Success([]),
        lambda key_source: lambda acc: acc + [key_source],
    ).map(lambda key_sources: dict(key_sources)).unwrap())

    result_futures = {}
    with ProcessPoolExecutor() as executor:
        for lh, rh in product(target_agents, chain(target_agents,
                                                   enemy_agents)):
            if lh == rh:
                continue

            lh, rh = _swap_agents_if_need(lh, rh)
            result_key = _get_result_key(lh, rh)
            lh_source = source_map[lh.key]
            rh_source = source_map[rh.key]
            result_futures[result_key] = executor.submit(
                _run_inner, lh_source, rh_source)

    return {k: f.result() for k, f in result_futures.items()}
예제 #3
0
파일: test_loop.py 프로젝트: timqsh/returns
def test_fold_loop(iterable, sequence):
    """Iterable for ``Result`` and ``FailFast``."""
    assert Fold.loop(iterable, sequence.from_value(10), _sum_two) == sequence
예제 #4
0
def test_fold_loop_recursion_limit():
    """Ensures that ``.loop`` method is recurion safe."""
    limit = sys.getrecursionlimit() + 1
    iterable = (IO(1) for _ in range(limit))
    assert Fold.loop(iterable, IO(0), _sum_two) == IO(limit)