animals = ["dog", "cat", "chicken", "sheep"]
animal_iterator = iter(animals)
class Squares:
def __init__(self):
self.i = 0
def __iter__(self):
return self
def __next__(self):
self.i += 1
return self.i*self.i
squares = Squares()
for i in squares: # Loops forever
print(i)
def simple_generator():
print("Hello")
yield 1
print("Nice to meet you")
yield 2
print("I am a generator")
def squares():
i = 0
while True:
i += 1
yield i*i
def fib():
a = 1
b = 1
while True:
yield a
a, b = b, a+b
module.exports = leftpad;
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}