def enqueue(self, el): """Returns new queue object with given element is added onto the end""" # check if we need to rebalance to prevent spikes if len(self.left) >= len(self.right): return Queue(self.left, self.right.cons(el)) left = reduce(lambda acc, el: acc.cons(el), self.right, self.left) return Queue(left, LinkedList().cons(el))
def _find_container(self, pos): if pos < 0 or pos > self.length: raise IndexError() if pos >= self._tailoff(): return self.tail bottom = reduce(lambda node, level: node.array[(pos >> level) & 0x01f], range(self.shift, 0, -5), self.root) return bottom.array
def test_cons_operation(self): v = Vector() self.assertEqual(0, len(v)) v1 = v.cons(10) self.assertEqual(1, len(v1)) self.assertEqual(0, len(v)) # previous value didn't change up = reduce(lambda acc, el: acc.cons(el), range(513), Vector()) self.assertEqual(513, len(up))
def __iter__(self): s = reduce( lambda acc, el: acc << ( el if isinstance(el, self.__class__) else [el] ), takewhile(lambda el: el is not None, self.array), Stream() ) return iter(s)
def unpackStrList(lists): def reduceStr(total, s): if isinstance(s, list): return total + s else: total.append(s) return total return list(reduce(reduceStr, lists, []))
def dequeue(self): """Return pair of values: the item from the front of the queue and the new queue object without poped element. """ if not self: raise ValueError("Queue is empty") # if there is at least one element on the left, we can return it if self.left: return self.left.head, Queue(self.left.tail, self.right) # in other case we need to copy right to left before d = reduce(lambda acc, el: acc.cons(el), self.right, LinkedList()) return d.head, Queue(d.tail, LinkedList())
def test_assoc_get_operations(self): v = Vector() v1 = v.assoc(0, 10) v2 = v1.assoc(1, 20) v3 = v2.assoc(2, 30) self.assertEqual(10, v3.get(0)) self.assertEqual(20, v3.get(1)) self.assertEqual(30, v3.get(2)) # check persistence v4 = v2.assoc(2, 50) self.assertEqual(30, v3.get(2)) self.assertEqual(50, v4.get(2)) # long vector up = reduce(lambda acc, el: acc.assoc(el, el*2), range(1500), Vector()) self.assertEqual(2800, up.get(1400)) self.assertEqual(2998, up.get(1499))
def test_vector_iterator(self): v = reduce(lambda acc, el: acc.assoc(el, el + 1), range(1500), Vector()) self.assertEqual(list(range(1, 1501)), list(v)) self.assertEqual(1125750, sum(v))
def test_repr_parse_str(self): self.assertEqual('=> ' + _, eval(repr('=> ' + _))) self.assertEqual( reduce(lambda f, n: f.format(n), ('({0} & _)',) * 11).format('_'), repr(reduce(_ & _, (_,) * 12)), )
# recursion again def reduce2(f, xs, start): if xs: return f(xs[0], reduce2(f, xs[1:], start)) else: return start def reduce3(f, xs, accumulator): if xs: return reduce3(f, xs[1:], f(xs[0], accumulator)) else: return accumulator falsify("reduce vs reduce1", lambda xs, s: reduce(add, xs, s) == reduce1(add, xs, s), [int], int) falsify("reduce1 vs reduce2", lambda xs, s: reduce1(add, xs, s) == reduce2(add, xs, s), [int], int) falsify("reduce2 vs reduce3", lambda xs, s: reduce2(add, xs, s) == reduce3(add, xs, s), [int], int) # higher order cont def doubles(xs): return map(lambda x: x * 2, xs) falsify("doubles", lambda xs: not xs or nth(doubles(xs), 0) == xs[0] * 2, [int]) # partial application def doubles1(xs): return map(partial(mul, 2), xs)
def test_index_error(self): v = reduce(lambda acc, el: acc.assoc(el, el+2), range(50), Vector()) self.assertRaises(IndexError, v.get, -1) self.assertRaises(IndexError, v.get, 50) self.assertRaises(IndexError, v.get, 52)
def test_vector_iterator(self): v = reduce(lambda acc, el: acc.assoc(el, el+1), range(1500), Vector()) self.assertEqual(list(range(1, 1501)), list(v)) self.assertEqual(1125750, sum(v))
def test_pop_operations(self): v = reduce(lambda acc, el: acc.cons(el), range(2000), Vector()) self.assertEqual(1999, len(v.pop())) self.assertEqual(list(range(1999)), list(v.pop()))
def test_index_error(self): v = reduce(lambda acc, el: acc.assoc(el, el + 2), range(50), Vector()) self.assertRaises(IndexError, v.get, -1) self.assertRaises(IndexError, v.get, 50) self.assertRaises(IndexError, v.get, 52)
def unpackList(lists): return list(reduce((lambda x, y: x + y), lists))
def test_repr_parse_str(self): self.assertEqual("=> " + _, eval(repr("=> " + _))) self.assertEqual( reduce(lambda f, n: f.format(n), ("({0} & _)",) * 11).format("_"), repr(reduce(_ & _, (_,) * 12)) )
def from_iterable(measure, it): tree = FingerTree.Empty(measure) return reduce(lambda acc, curr: acc.push_front(curr), it, tree)
def filter4(pred, xs): return reduce(lambda ys, y: ys + [y] if pred(y) else ys, xs, [])
def map4(f, xs): return reduce(lambda ys, y: ys + [f(y)], xs, [])
def fac1(n): return reduce(mul, range(1, n + 1), 1)
def __iter__(self): s = reduce( lambda acc, el: acc << (el if isinstance(el, self.__class__) else [el]), takewhile(lambda el: el is not None, self.array), Stream()) return iter(s)
def test_repr_parse_str(self): self.assertEqual('=> ' + _, eval(repr('=> ' + _))) self.assertEqual( reduce(lambda f, n: f.format(n), ('({0} & _)', ) * 11).format('_'), repr(reduce(_ & _, (_, ) * 12)), )
def table_rows(row_dicts): return reduce(add, map(table_row, row_dicts))