def create(): def func(acc, x): if acc is None: acc = 0 return of(acc + x) return xs.pipe(_.merge_scan(func))
def _files_in_path(credentials, path, retries): def find_with_parent(parent, name): return (list_folder(credentials, parent, name_filter=name, retries=retries) if parent else of([])).pipe( first(), # There might be more than one file with the same name - we just take the first map(lambda files: files[0] if len(files) else None) ) return _path_elements(path).pipe( merge_scan(find_with_parent, _root_folder) )
def create_folder_with_path(credentials, path: str): def create_if_missing(parent, name_file): name = name_file[0] file = name_file[1] if file: return of(file) else: return create_folder(credentials, parent, name) return zip( _path_elements(path), _files_in_path(credentials, path), ).pipe(merge_scan(create_if_missing, _root_folder), last())
def test_merge_scan_concurrent(self): accumulator_calls = [0] def accumulator(acc, x): accumulator_calls[0] += 1 return of(acc + x) xs = from_list([1, 2, 3, 4, 5]).pipe(_.merge_scan(accumulator, seed=0)) results = TestSubscriber(xs).results() assert len(results) == 6 assert results[0].value.kind == 'N' and results[0].value.value == 1 assert results[1].value.kind == 'N' and results[1].value.value == 1 + 2 assert results[2].value.kind == 'N' and results[ 2].value.value == 1 + 2 + 3 assert results[3].value.kind == 'N' and results[ 3].value.value == 1 + 2 + 3 + 4 assert results[4].value.kind == 'N' and results[ 4].value.value == 1 + 2 + 3 + 4 + 5 assert results[5].value.kind == 'C' assert accumulator_calls[ 0] == 5 # Accumulator os not called more often than needed
def create(): return xs.pipe(_.merge_scan(lambda acc, x: of(acc + x), seed=seed))
def create(): return xs.pipe(_.merge_scan(seed, lambda acc, x: of(acc + x)))
def create(): def func(acc, x): return of(acc + x) return never().pipe(_.merge_scan(seed=seed, accumulator=func))
def create(): return never().pipe(_.merge_scan(lambda acc, x: of(acc + x)))