예제 #1
0
    def test_chunk(self):
        lorem = [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
            "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
            "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
            "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]

        seq = collections.Counter(w for sentence in lorem
                                  for w in sentence.split())
        distinct = len(seq)

        # (Broken) Count distinct words
        self.assertGreaterEqual(
            Stream(lorem).flatmap(lambda sentence: sentence.split()).
            chunk_by_key(lambda x: hash(x) % 10).reduce_once(
                lambda chunk: len(set(chunk))).sum(), distinct)

        # Count distinct words (also broken)
        def add(s1, s2):
            s1.update(s2)
            return s1

        self.assertGreaterEqual(
            Stream(lorem).flatmap(lambda sentence: sentence.split(
            )).chunk_by_key(lambda w: hash(w) % 10).collect(set, add, len),
            distinct)

        # Count distinct words
        self.assertEqual(
            Stream(lorem).flatmap(lambda sentence: sentence.split()).
            chunk_by_key(lambda w: hash(w) % 10).collect(
                collections.Counter, add), seq)
예제 #2
0
    def test_count(self):
        self.assertEqual(Stream(range(10**6)).count(), 10**6)
        self.assertEqual(SequentialStream(range(10**6)).count(), 10**6)

        self.assertEqual(
            Stream(range(10**3)).flatmap(
                lambda x: SequentialStream(range(x, x + 1000))).count(), 10**6)
        self.assertEqual(
            Stream(range(10**3)).flatmap(
                lambda x: EagerStream(range(x, x + 1000))).count(), 10**6)
예제 #3
0
    def test_lines(self):
        with open('words') as file:
            chars = ''.join(file)
        self.assertTrue(
            Stream().lines(open('words')).flatmap(tuple).take_one() in chars)

        self.assertTrue(
            list(Stream().lines(open('words')).flatmap(tuple))[0] in chars)

        self.assertEqual(Stream().lines(open('words')).flatmap(tuple).count(),
                         len(chars))
예제 #4
0
def test2():
    t = time.time()
    print(repr(Stream().lines(open('words')).flatmap(tuple).take_one()))
    print('elapsed:', time.time() - t)

    t = time.time()
    print(repr(list(Stream(open('words')).flatmap(tuple))[0]))
    print('elapsed:', time.time() - t)

    # Count characters in a file
    t = time.time()
    print(Stream(open('words')).flatmap(tuple).count())
    print('elapsed:', time.time() - t)
예제 #5
0
def test3():
    print(
        list(
            Stream(range(10**3)).flatmap(
                lambda x: EagerStream(range(x, x + 1000))).take_stream(5)))
    print(Stream(range(10**6)).count())
    print(SequentialStream(range(10**6)).count())
    print(
        Stream(range(10**3)).flatmap(
            lambda x: SequentialStream(range(x, x + 1000))).count())
    print(
        Stream(range(
            10**3)).flatmap(lambda x: EagerStream(range(x, x + 1000))).count())
    print(
        Stream(range(10**3)).flatmap(
            lambda x: EagerStream(range(x, x + 1000))).take_one())
예제 #6
0
 def queens_(todo, tail, depth=0):
     if not todo:
         return [tail]
     return ((Stream(todo) if depth == 0 else
              SequentialStream(todo)).filter(lambda r: safe(r, tail))
             # .filter(partial(safe, tail=tail))
             .flatmap(wrap_q(todo, tail)))
예제 #7
0
    def test_take(self):

        self.assertGreaterEqual(
            len(
                list(
                    Stream(range(10**3)).flatmap(lambda x: EagerStream(
                        range(x, x + 1000))).take_stream(5))), 5)

        self.assertEqual(
            len(
                list(
                    Stream(range(10**3)).flatmap(
                        lambda x: EagerStream(range(x, x + 1000))).take(5))),
            5)

        self.assertLess(
            Stream(range(10**3)).flatmap(
                lambda x: EagerStream(range(x, x + 1000))).take_one(), 10**3)
예제 #8
0
def benchmark():
    import time

    def isprime(n):
        return not any(n % p == 0 for p in range(2, n))

    N = 3 * 10**3

    t = time.time()
    print(Stream(range(2, N)).flatmap(range).filter(isprime).sum())
    print('sum', time.time() - t)

    t = time.time()
    print(
        Stream(range(2, N)).flatmap(lambda x: range(x)).filter(isprime).reduce(
            (lambda x, y: x + y), 0))
    print('reduce', time.time() - t)

    t = time.time()
    print(sum(x for a in range(N) for x in range(a) if isprime(x)))
    print('seq', time.time() - t)
예제 #9
0
    def test_filter(self):
        N = 1000
        seq = sum(filter((lambda x: x % 10 == 0), range(N)))

        self.assertEqual(
            SequentialStream(range(N)).filter(lambda x: x % 10 == 0).sum(),
            seq)

        self.assertEqual(
            FunctionalStream(range(N)).filter(lambda x: x % 10 == 0).sum(),
            seq)

        self.assertEqual(
            Stream(range(N)).filter(lambda x: x % 10 == 0).sum(), seq)
예제 #10
0
def test():
    N = 1000

    print(sum(filter((lambda x: x % 10 == 0), range(N))))

    print(SequentialStream(range(N)).filter(lambda x: x % 10 == 0).sum())

    print(FunctionalStream(range(N)).filter(lambda x: x % 10 == 0).sum())

    print(Stream(range(N)).filter(lambda x: x % 10 == 0).sum())

    print(Stream(range(N)).map(lambda x: x == 30).any())
    print(Stream(range(N)).map(lambda x: x == -30).any())
    print(Stream(range(N)).map(lambda x: x == 30).all())
    print(Stream(range(N)).map(lambda x: x <= N).all())

    lorem = [
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
        "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
        "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
        "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    ]

    # (Broken) Count distinct words
    print(
        Stream(lorem).flatmap(lambda sentence: sentence.split()).chunk_by_key(
            lambda x: hash(x) % 10).reduce_once(
                lambda chunk: len(set(chunk))).sum())

    # Count distinct words
    def add(s1, s2):
        s1.update(s2)

    return s1
    print(
        Stream(lorem).flatmap(lambda sentence: sentence.split()).chunk_by_key(
            lambda w: hash(w) % 10).collect(set, add, len))

    # Count distinct words
    print(
        Stream(lorem).flatmap(lambda sentence: sentence.split()).chunk_by_key(
            lambda w: hash(w) % 10).collect(collections.Counter, add))
예제 #11
0
 def perms_(todo, tail, depth=0):
     if not todo:
         return [tail]
     return ((Stream(todo) if depth == 0 else
              FunctionalStream(todo)).flatmap(wrap_p(todo, tail)))
예제 #12
0
 def test_any(self):
     N = 1000
     self.assertTrue(Stream(range(N)).map(lambda x: x == 30).any())
     self.assertFalse(Stream(range(N)).map(lambda x: x == -30).any())
     self.assertFalse(Stream(range(N)).map(lambda x: x == 30).all())
     self.assertTrue(Stream(range(N)).map(lambda x: x <= N).all())
예제 #13
0
 def test_peek():
     N = 10**2
     q = mp.Queue()
     s = Stream(range(N)).peek(lambda x: q.put(x)).sum()
     self.assertEqual(s, sum(range(N)))
     self.assertEqual(sorted(read_queue(q, N)), list(range(N)))
예제 #14
0
 def test_foreach(self):
     N = 10**2
     q = mp.Queue()
     Stream(range(N)).foreach(lambda x: q.put(x))
     self.assertEqual(sorted(read_queue(q, N)), list(range(N)))