Exemplo n.º 1
0
 def testExtend(self):
     s = Stream.number().filter(lambda i: i % 3 == 0).take(3).extend(
         Stream.number().filter(lambda i: i % 3 == 1).take(3),
         Stream.number().filter(lambda i: i % 3 == 2).take(3))
     l = s.to_list()
     self.assertEqual(9, len(l))
     self.assertEqual(8, l[-1])
Exemplo n.º 2
0
 def testAnyAll(self):
     self.assertFalse(
         Stream.number(step=2).take(10).any(lambda x: x % 2 != 0))
     self.assertTrue(
         Stream.number(step=2).take(10).all(lambda x: x % 2 == 0))
     self.assertFalse(Stream.empty().any(lambda x: x > 0))
     self.assertTrue(Stream.empty().all(lambda x: x > 0))
Exemplo n.º 3
0
 def testTakeWhile(self):
     self.assertEqual(
         8,
         self.countIter(Stream.number(step=2).take_while(lambda i: i < 15)))
     self.assertEqual(
         14, max(Stream.number(step=2).take_while(lambda i: i < 15)))
     self.assertEqual(
         0, max(Stream.number(step=2).take_while(lambda i: i < 1)))
Exemplo n.º 4
0
    def testForeach(self):
        count = {"count": 0}

        def addCount(x):
            count["count"] += x

        Stream.number().map(lambda x: x**2).filter(lambda x: x % 3 == 0).take(
            3).foreach(lambda x: addCount(x))
        self.assertEqual(45, count["count"])
Exemplo n.º 5
0
 def testConcat(self):
     l = Stream.concat(
         Stream.number().filter(lambda i: i % 3 == 0).take(3),
         Stream.number().filter(lambda i: i % 3 == 1).take(3),
         Stream.number().filter(lambda i: i % 3 == 2).take(3)).to_list()
     self.assertEqual(9, len(l))
     self.assertEqual(8, l[-1])
     l = Stream.concat(Stream.number().filter(lambda i: i % 3 == 0).take(3),
                       range(3)).to_list()
     self.assertEqual(6, len(l))
Exemplo n.º 6
0
    def testFold(self):
        self.assertEqual(45,
                         Stream.number().take(10).fold(lambda x, y: x + y, 0))
        self.assertEqual(
            "01234",
            Stream.number().take(5).fold(lambda x, y: x + str(y), ""))

        self.assertTrue(45, Stream.number().take(10).sum())
        self.assertEqual(
            "01234",
            Stream.number().take(5).map(lambda i: str(i)).join(""))
Exemplo n.º 7
0
    def testZip(self):
        l = Stream.zip(range(5),
                       Stream.number(start=4, step=-1).take(8)).to_list()
        self.assertEqual(5, len(l))
        self.assertTrue(
            Stream(l).map(lambda tup: tup[0] + tup[1]).all(lambda x: x == 4))

        l = Stream.number(start=4,
                          step=-1).take(8).zip_with(range(5)).to_list()
        self.assertEqual(5, len(l))
        self.assertTrue(
            Stream(l).map(lambda tup: tup[0] + tup[1]).all(lambda x: x == 4))
Exemplo n.º 8
0
    def testToMap(self):
        m = Stream.zip(range(5),
                       Stream.number(start=4, step=-1).take(3)).to_map()
        self.assertEqual(3, len(m))
        self.assertEqual(4, m[0])
        self.assertEqual(3, m[1])
        self.assertEqual(2, m[2])

        m = Stream.number().take(3).to_map(key=lambda it: it,
                                           value=lambda it: it * 2)
        self.assertEqual(2, m[1])
        self.assertEqual(4, m[2])
        self.assertEqual(0, m[0])
Exemplo n.º 9
0
 def testFilter(self):
     self.assertEqual(
         12, sum(Stream.number().filter(lambda i: i % 2 == 0).take(4)))
Exemplo n.º 10
0
 def testTake(self):
     s = Stream.number(step=2)
     self.assertEqual(4, self.countIter(s.take(4)))
     self.assertEqual(0, self.countIter(s.take(0)))
     self.assertEqual(3, self.countIter(s.take(3).take(4)))
Exemplo n.º 11
0
 def testInfinite(self):
     s = Stream.number(step=2)
     for i in range(100):
         self.assertEqual(i * 2, s.next())
Exemplo n.º 12
0
 def testFlat(self):
     l = Stream.number(
         step=3).map(lambda x: (x, x + 1, x + 2)).flat().take(10).to_list()
     self.assertEqual(45, sum(l))
Exemplo n.º 13
0
 def testprepend(self):
     l = Stream.number().take(4).prepend(99, 100, 101).to_list()
     self.assertEqual(7, len(l))
     self.assertEqual(1, l[-3])
     self.assertEqual(99, l[0])
Exemplo n.º 14
0
 def testAppend(self):
     l = Stream.number().take(4).append(99, 100, 101).to_list()
     self.assertEqual(7, len(l))
     self.assertEqual(101, l[-1])
Exemplo n.º 15
0
 def testTolist(self):
     self.assertEqual([1, 3, 5],
                      Stream.number(start=1, step=2).take(3).to_list())
Exemplo n.º 16
0
 def testNextItem(self):
     self.assertEqual(3, Stream.number(start=3).next_item().get())
     self.assertEqual(100, Stream.empty().next_item().or_else(100))
Exemplo n.º 17
0
 def testMap(self):
     self.assertEqual(16, max(Stream.number().map(lambda x: x**2).take(5)))