def test_reduce_sum_non_empty(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4, 5, 6]) plst = PList.from_seq(slst) exp = 21 res = plst.reduce(operator.add, 0) assert res == exp
def test_reduce_cons(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4]) plst = PList.from_seq(slst) res = plst.reduce(operator.add) exp = 10 assert res == exp
def test_map_from_seq(): # pylint: disable=missing-docstring data = PList.from_seq(MSG) data.invariant() res = data.map(upper_case).to_seq() exp = list(upper_case(MSG)) assert res == exp
def randint(min_, max_): """ Returns the same random number on all the processors. :param min_: int :param max_: int :return: int """ return PList.from_seq([random.randint(min_, max_)]).to_seq()[0]
def test_map_reduce_cons(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4]) plst = PList.from_seq(slst) exp = plst.map(fun.incr) exp = exp.reduce(operator.add) res = plst.map_reduce(fun.incr, operator.add) assert res == exp
def test_map_reduce_nil(): # pylint: disable=missing-docstring neutral = 0 slst = SList() plst = PList.from_seq(slst) res = plst.map_reduce(fun.incr, operator.add, neutral) exp = neutral assert res == exp
def test_reduce_sum_empty(): # pylint: disable=missing-docstring neutral = 0 slst = SList() plst = PList.from_seq(slst) exp = neutral res = plst.reduce(operator.add, neutral) assert res == exp
def generate_plist(function, start=0): """ Generates a parallel list of random size between 1 and 111, or an empty list, using ``function`` to initialize the elements of the list. If ``start`` is 1, a non-empty parallel list is generated. :param function: callable :param start: int (0 or 1) :return: PList """ choice = randint(start, 2) size = randint(1, 111) if choice == 0: return PList() if choice == 1: return PList.init(function, size) return PList.from_seq([function(i) for i in range(0, size)])
def __main(): parallel_list1 = PList.init(lambda i: __MSG[i], len(__MSG)) parallel_list2 = PList.init(lambda x: x, len(__MSG)) parallel_list4 = parallel_list1.map(lambda x: x.capitalize()).zip( parallel_list2) parallel_list6 = parallel_list4.map(lambda x: x[0]).mapi(lambda i, x: (i, x)) parallel_list7 = parallel_list6.map(lambda x: x[1]) parallel_list8 = parallel_list7.map(lambda x: 1) size = parallel_list8.reduce(lambda x, y: x + y, 0) parallel_list9 = parallel_list7.get_partition() parallel_list10 = parallel_list9.map( lambda l: SList(l).filter(lambda c: c != 'O')).flatten() parallel_list11 = PList.from_seq(["Hello World!"]) filtered = SList(parallel_list10.get_partition().reduce(concat, [])).reduce(add) str1 = SList(parallel_list9.reduce(add)).reduce(add) str2 = parallel_list11.to_seq()[0] par.at_root(lambda: print(f'Capitalized: \t{str1}\n' f'Identity:\t{str2}\n' f'Length:\t\t{size}\n' f'Filtered:\t{filtered}'))