def test_fib_infinite_stream(self): from operator import add f = Stream() fib = f << [0, 1] << iters.map(add, f, iters.drop(1, f)) self.assertEqual([0,1,1,2,3,5,8,13,21,34], list(iters.take(10, fib))) self.assertEqual(6765, fib[20]) self.assertEqual([832040,1346269,2178309,3524578,5702887], fib[30:35])
def test_fib_infinite_stream(self): from operator import add f = Stream() fib = f << [0, 1] << iters.map(add, f, iters.drop(1, f)) self.assertEqual([0,1,1,2,3,5,8,13,21,34], list(iters.take(10, fib))) self.assertEqual(6765, fib[20]) self.assertEqual([832040,1346269,2178309,3524578,5702887], list(fib[30:35])) # 35 elements should be already evaluated self.assertEqual(fib.cursor(), 35)
def test_take(self): self.assertEqual([0, 1], list(iters.take(2, range(10)))) self.assertEqual([0, 1], list(iters.take(10, range(2))))
def test_unfold_infinite(self): doubler = op.unfold(lambda x: (x*2, x*2)) self.assertEqual(20, next(doubler(10))) self.assertEqual([20, 40, 80, 160, 320], list(iters.take(5, doubler(10))))
def test_take(self): self.assertEqual([0,1], list(iters.take(2, range(10)))) self.assertEqual([0,1], list(iters.take(10, range(2))))
def test_unfold_infinite(self): doubler = op.unfold(lambda x: (x * 2, x * 2)) self.assertEqual(20, next(doubler(10))) self.assertEqual([20, 40, 80, 160, 320], list(iters.take(5, doubler(10))))