Code Explainers

Browse the library

rust
use std::time::{Duration, Instant};
 
pub struct TokenBucket {
    capacity: f64,

A token bucket rate limiter in Rust

rate-limiting token-bucket lazy-refill
Intermediate 8 steps
java
@RestController
@RequestMapping("/api/users")
public class UserController {
 

Bean Validation in a Spring REST controller

validation rest-api exception-handling
Intermediate 9 steps
go
package server
 
import (
	"context"

Composing HTTP middleware in Go

middleware http-handlers closures
Intermediate 8 steps
ruby
def build_tree(rows, root_id: nil)
  children_by_parent = Hash.new { |hash, key| hash[key] = [] }
 
  rows.each do |row|

Building a tree from flat rows in Ruby

recursion hash-grouping tree-structure
Intermediate 5 steps
php
<?php
 
namespace App\Http\Controllers;
 

Verifying Stripe webhooks in Laravel

webhooks signature-verification event-dispatch
Intermediate 8 steps
typescript
interface Page<T> {
  items: T[];
  nextCursor: string | null;
}

Streaming cursor pagination with async generators

async-generators pagination generics
Intermediate 8 steps
javascript
const asyncHandler = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};
 

Async error handling in Express routes

async-await error-handling middleware
Intermediate 7 steps
python
from django.core.cache import cache
from rest_framework.throttling import SimpleRateThrottle
 
 

A login rate throttle in Django REST Framework

rate-limiting caching throttling
Intermediate 8 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    routing::get,

Building a JSON user API in Axum

routing shared-state json-serialization
Intermediate 8 steps
java
@Service
public class OrderService {
 
    private final OrderRepository orderRepository;

Decoupling side effects with Spring events

event-driven transactions decoupling
Intermediate 8 steps
rust
use std::time::Duration;
 
use axum::{
    http::{header, HeaderValue, Method},

Configuring CORS on an Axum Router

cors middleware http-headers
Intermediate 7 steps
python
import stripe
from fastapi import APIRouter, Request, Header, HTTPException
 
from app.config import settings

Handling Stripe webhooks in FastAPI

webhooks signature-verification event-routing
Intermediate 7 steps
typescript
type RequestState<T, E = string> =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "success"; data: T; fetchedAt: number }

Modeling request state with discriminated unions

discriminated-unions exhaustiveness-checking state-machine
Intermediate 8 steps
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) {
    return orders.stream()
            .collect(Collectors.groupingBy(Order::getCustomerId));
}

Grouping streams with Java Collectors

streams grouping collectors
Intermediate 5 steps
php
<?php
 
namespace App\Http\Controllers;
 

Building a filtered product index in Laravel

query-builder validation conditional-queries
Intermediate 9 steps
ruby
class MetricSeries
  def initialize(readings, window: 5)
    @readings = readings
    @window = window

Rolling averages with each_cons in Ruby

sliding-window enumerable data-smoothing
Intermediate 7 steps
javascript
async function mapWithConcurrency(items, limit, worker) {
  const results = new Array(items.length);
  let nextIndex = 0;
 

Bounded-concurrency async map in JavaScript

concurrency async-await promises
Intermediate 7 steps
go
package config
 
import (
	"encoding/json"

Parsing untyped JSON config in Go

json parsing type assertions error handling
Intermediate 7 steps