Code Explainers

Code explainers tagged #generators

php
<?php
 
declare(strict_types=1);
 

Streaming a fixed-width file into typed objects

generators value-objects file-parsing
Intermediate 8 steps
python
from contextlib import contextmanager
from typing import Iterator
 
import psycopg2

Streaming Postgres rows with a server-side cursor

generators context-managers database-streaming
Intermediate 7 steps
python
import json
import time
import queue
 

Server-Sent Events streaming in Flask

server-sent-events streaming pub-sub
Advanced 9 steps
python
import re
from collections import Counter
from pathlib import Path
 

Ranking the top words in a PDF

text-processing regex counting
Intermediate 6 steps
typescript
function* map<T, U>(source: Iterable<T>, fn: (value: T, index: number) => U): Generator<U> {
  let index = 0;
  for (const value of source) {
    yield fn(value, index++);

Building a lazy iterator pipeline in TypeScript

generators lazy-evaluation iterators
Intermediate 7 steps
python
import csv
 
from django.http import StreamingHttpResponse
from django.utils.timezone import now

Streaming a CSV export in Django

streaming csv-export generators
Intermediate 7 steps
python
import ijson
from decimal import Decimal
 
 

Streaming JSON aggregation with ijson

streaming json-parsing generators
Intermediate 7 steps
python
import time
from contextlib import contextmanager
 
 

A reusable timing context manager in Python

context-manager timing generators
Intermediate 5 steps
php
<?php
 
namespace App\Console\Commands;
 

Streaming CSV imports in Laravel

lazy-evaluation generators batch-processing
Intermediate 7 steps
ruby
def collatz_lengths
  Enumerator.new do |y|
    n = 1
    loop do

Building infinite sequences with lazy enumerators

lazy-evaluation enumerators infinite-sequences
Intermediate 8 steps
php
final class UserActivityStream
{
    public function __construct(
        private readonly \PDO $db,

Streaming user activity with PHP generators

generators pagination lazy-evaluation
Intermediate 8 steps
python
from itertools import islice
from typing import Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Batching an iterable for bulk indexing

generators batching lazy-evaluation
Intermediate 7 steps
python
from collections.abc import Mapping
from typing import Any, Iterator
 
 

Flattening nested config into dotted keys

recursion generators tree-traversal
Intermediate 7 steps
python
import csv
import io
from datetime import datetime
 

Streaming a CSV export in Flask

streaming generators csv
Intermediate 9 steps
php
<?php
 
declare(strict_types=1);
 

Streaming file lines with a PHP generator

generators lazy-evaluation file-io
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Callable, Hashable, Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Two ways to dedupe while keeping order

deduplication generators ordered-data
Intermediate 7 steps
python
import re
from datetime import datetime
from dataclasses import dataclass
 

Parsing access logs with named regex groups

regex named-groups dataclasses
Intermediate 6 steps
python
import gzip
from pathlib import Path
from typing import Iterator
 

Streaming TSV records with Python generators

generators lazy-evaluation file-io
Intermediate 5 steps