# 151. Generators for Lazy Evaluation

Here are 10 Python code snippets demonstrating the use of generators for lazy evaluation. Each snippet is separated by a delimiter.

***

**Snippet 1: Generating an infinite sequence of numbers**

```python
def infinite_numbers():
    num = 0
    while True:
        yield num
        num += 1

# Usage
gen = infinite_numbers()
for i in range(5):
    print(next(gen))  # Prints numbers 0 to 4
```

***

**Snippet 2: Lazily reading a large file line by line**

```python
def read_large_file(filename):
    with open(filename, 'r') as file:
        for line in file:
            yield line.strip()

# Usage
for line in read_large_file('large_file.txt'):
    print(line)
```

***

**Snippet 3: Filtering even numbers from a large range**

```python
def even_numbers(numbers):
    for num in numbers:
        if num % 2 == 0:
            yield num

# Usage
numbers = range(1000)
for even in even_numbers(numbers):
    print(even)
```

***

**Snippet 4: Generating Fibonacci sequence**

```python
def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# Usage
fib_gen = fibonacci()
for _ in range(10):
    print(next(fib_gen))
```

***

**Snippet 5: Lazily combining two lists element by element**

```python
def combine_lists(list1, list2):
    for x, y in zip(list1, list2):
        yield x + y

# Usage
list1 = [1, 2, 3]
list2 = [4, 5, 6]
for result in combine_lists(list1, list2):
    print(result)
```

***

**Snippet 6: Reading a file in chunks**

```python
def read_in_chunks(file_name, chunk_size=1024):
    with open(file_name, 'rb') as file:
        while chunk := file.read(chunk_size):
            yield chunk

# Usage
for chunk in read_in_chunks('large_file.bin', 1024):
    print(chunk)
```

***

**Snippet 7: Lazily transforming data from a list**

```python
def square_numbers(numbers):
    for num in numbers:
        yield num ** 2

# Usage
numbers = [1, 2, 3, 4, 5]
for square in square_numbers(numbers):
    print(square)
```

***

**Snippet 8: Lazy Cartesian product of two lists**

```python
def cartesian_product(list1, list2):
    for x in list1:
        for y in list2:
            yield (x, y)

# Usage
for pair in cartesian_product([1, 2], ['a', 'b']):
    print(pair)
```

***

**Snippet 9: Lazy prime number generator**

```python
def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True

def prime_numbers():
    num = 2
    while True:
        if is_prime(num):
            yield num
        num += 1

# Usage
prime_gen = prime_numbers()
for _ in range(10):
    print(next(prime_gen))
```

***

**Snippet 10: Processing a large JSON object lazily**

```python
import json

def lazy_json_reader(filename):
    with open(filename, 'r') as file:
        for line in file:
            yield json.loads(line)

# Usage
for obj in lazy_json_reader('large_data.json'):
    print(obj)
```

***

Let me know if you'd like more examples or detailed explanations of any snippet!
