def zip(self) -> None: a = 1 b = 2 ab = (a, b) l1 = LazyList((a, a, a, a), chunk_size=1) l2 = LazyList((b, b, b, b), chunk_size=1) l1[1] z = l1.zip(l2) z.strict.should.equal(List(ab, ab)) z.drain.should.equal(List(ab, ab, ab, ab))
def episode_enumeration_match(filename): custom_rex = ConfigClient('series')('enumeration_regex') if custom_rex: return re.search(custom_rex, str(filename)) else: if isinstance(filename, Path): filename = filename.name searcher = lambda s: re.search(s, filename, re.I) rex = LazyList(map(searcher, regexes)) return rex.find(lambda a: a) | None
def search(self, monitor): return ( LazyList(self._queries(monitor)) .filter(_.valid) .apzip(self._safe_search) .map2(L(SearchResults)(_, _, self.min_seeders, self.max_size)) )
def collect(self) -> None: def f(a: int, n: str) -> Maybe[str]: return Just(n) if a % 2 == 0 else Nothing l: LazyList = LazyList((a, str(a)) for a in range(10)) l2 = l.collect(tupled2(f)) l2.drain.should.equal(List('0', '2', '4', '6', '8'))
def zip(self, fa: LazyList[A], fb: LazyList[B], *fs) -> LazyList: fss = (fa, fb) + fs maxlen = max(map(lambda a: len(a.strict), fss)) for f in fss: f._fetch(maxlen - 1) stricts = map(_.strict, fss) strict = ListZip().zip(*stricts) return LazyList(self._zip(fss), init=strict)
def slice_finite(self) -> None: l = LazyList(range(30), chunk_size=20) l[:15].should.have.length_of(15) l.strict.should.have.length_of(20) l[:20].should.have.length_of(20) l.strict.should.have.length_of(20) l[:21].should.have.length_of(21) l.strict.should.have.length_of(30)
def slice_infinite(self) -> None: l = LazyList(itertools.count(), chunk_size=20) l[:15].should.have.length_of(15) l.strict.should.have.length_of(20) l[:20].should.have.length_of(20) l.strict.should.have.length_of(20) l[:21].should.have.length_of(21) l.strict.should.have.length_of(40)
def flat_map(self, fa: LazyList[A], f: Callable[[A], LazyList[B]]) -> LazyList[B]: a, b = itertools.tee(fa.source) fa.source = a strict_m = fa.strict.map(f) lazy_m = map(f, b) mapped = itertools.chain(strict_m, lazy_m) source = itertools.chain.from_iterable(mapped) return LazyList(source, List(), fa._chunk_size)
def filter(self) -> None: l = LazyList(range(30)) l2 = l.filter(_ % 2 == 0) l2.strict.should.have.length_of(0) l3 = LazyList(range(30)) l3[29] l4 = l3.filter(_ % 2 == 0) l4.strict.should.have.length_of(15) l4.drain.should.equal(List.wrap(range(0, 30, 2)))
def apzip(self) -> None: l = LazyList((1, 2, 3), chunk_size=1) l[0] z = l.apzip(_ + 2) z.drain.should.equal(List((1, 3), (2, 4), (3, 5)))
def traverse_task(self) -> None: n = 3 l = LazyList(range(n)) result = l.traverse(Task.now, Task).attempt / _.drain result.should.contain(l.drain)
def sequence(self) -> None: n = 3 l = LazyList(map(Just, range(n))) target = LazyList(List.wrap(range(n))) (l.sequence(Maybe) / _.drain).should.contain(target.drain)
def traverse_io(self) -> None: n = 3 l = LazyList(range(n)) result = l.traverse(IO.now, IO).attempt / _.drain result.should.contain(l.drain)
def find(self) -> None: l = LazyList(range(30), chunk_size=20) l.find(_ == 21).should.contain(21) l.find(_ == 49).should.be.empty
def index_of(self) -> None: l = LazyList(range(30), chunk_size=20) l.index_of(21).should.contain(21) l.index_of(49).should.be.empty
def with_index(self) -> None: l = LazyList(itertools.count(), chunk_size=20) l2 = l.map(_ * 5).with_index l2[:2].should.equal(List((0, 0), (1, 5)))
def map(self) -> None: l = LazyList(itertools.count(), chunk_size=20) l.lift(5) l2 = l.map(_ * 10) l2[:5].should.equal(List.wrap(range(0, 50, 10)))
def single(self) -> None: l = LazyList(range(30), chunk_size=20) l[19].should.equal(19) l.strict.should.have.length_of(20) l[20].should.equal(20) l.strict.should.have.length_of(30)
def pure(self, a: A): return LazyList([], List(a))
def deep(self) -> None: n = int(1e4) l = LazyList(List.wrap(range(n))) l.index_of(n - 1).should.contain(n - 1)
def with_index(self, fa: LazyList[A]) -> List[Tuple[int, A]]: return LazyList(enumerate(fa.source), fa.strict, fa._chunk_size)
def nodes(*s: Tuple[Data, Callable[[RoseTree[Data]], RoseTree[Data]]] ) -> Callable[[RoseTree[Data]], LazyList[RoseTree[Data]]]: return lambda parent: LazyList(s).map2(node).map(lambda f: f(parent))
def sub_l(self) -> LazyList[Node[Data, Any]]: return LazyList([])
def flat_map(self) -> None: l = LazyLists.cons(LazyList((1, 2, 3)), LazyList((4, 5, 6)), LazyList((1, 2, 3))) l.lift(1) l.flat_map(I).drain.should.equal(List(1, 2, 3, 4, 5, 6, 1, 2, 3))
def leaves(*data: Data) -> Callable[[RoseTree[Data]], LazyList[RoseTree[Data]]]: return lambda parent: LazyList(data).map(leaf).map(lambda f: f(parent))
def fold_left(self) -> None: LazyList((1, 2, 3)).fold_left('')(lambda a, b: str(b) + a)\ .should.equal('321')
def strings(self) -> LazyList[str]: return LazyList([])
def fold_map(self) -> None: LazyList((1, 2, 3)).fold_map(5, _ * 2).should.equal(17)