Code Explainers

Code explainers tagged #lazy-evaluation

python
from django.db.models import Q
 
from .models import Order
 

DISTINCT ON queries in Django

orm querysets postgresql
Intermediate 7 steps
python
from django.core.paginator import Paginator
from django.shortcuts import render
 
from .models import Product

Filtering and paginating a Django product list

queryset pagination filtering
Intermediate 7 steps
php
<?php
 
declare(strict_types=1);
 

Streaming a fixed-width file into typed objects

generators value-objects file-parsing
Intermediate 8 steps
ruby
class QueryProxy
  ALLOWED = %i[where limit offset order includes distinct].freeze
 
  def initialize(relation, operations = [])

Building a lazy query proxy in Ruby

metaprogramming method_missing lazy-evaluation
Advanced 7 steps
rust
pub struct Fibonacci {
    current: u64,
    next: u64,
}

A Fibonacci iterator in Rust

iterators traits lazy-evaluation
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
javascript
class TokenBucket {
  constructor({ capacity, refillPerSecond }) {
    this.capacity = capacity;
    this.refillPerSecond = refillPerSecond;

How token-bucket rate limiting works in Express

rate-limiting token-bucket middleware
Intermediate 7 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
typescript
interface TokenBucketOptions {
  capacity: number;
  refillPerSecond: number;
}

How a token bucket rate limiter works

rate-limiting token-bucket lazy-evaluation
Intermediate 7 steps
php
<?php
 
declare(strict_types=1);
 

Streaming file lines with a PHP generator

generators lazy-evaluation file-io
Intermediate 7 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
ruby
module Fibonacci
  CACHE = Hash.new do |cache, n|
    cache[n] = cache[n - 1] + cache[n - 2]
  end

Memoizing Fibonacci with a Hash default block

memoization recursion hash-default
Intermediate 5 steps
rust
/// A custom iterator that yields Fibonacci numbers up to a maximum value.
pub struct Fibonacci {
    current: u64,
    next: u64,

Building a custom Fibonacci iterator in Rust

iterators traits state-machine
Intermediate 6 steps
javascript
async function* fetchPages(baseUrl, maxPages) {
  let page = 1;
  while (page <= maxPages) {
    const data = await mockFetch(`${baseUrl}?page=${page}`);

Paginated APIs with async generators

async-iterators generators pagination
Advanced 7 steps