Code Explainers

Code explainers tagged #lazy-evaluation

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