Code Explainers

Browse the library

python
from typing import List, Tuple
 
 
def merge_intervals(intervals: List[Tuple[int, int]]) -> List[Tuple[int, int]]:

How interval merging works

sorting greedy intervals
Intermediate 6 steps
java
public final class HmacJwt {
 
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();

How HMAC-signed JWTs are created and verified

jwt hmac cryptography
Intermediate 9 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 Pipeline
  def initialize
    @middlewares = []
  end

Building a middleware pipeline in Ruby

closures middleware composition
Advanced 8 steps
typescript
import { Pipe, PipeTransform, Inject, LOCALE_ID } from '@angular/core';
 
interface MoneyOptions {
  currency?: string;

Building a cached money pipe in Angular

pipes memoization internationalization
Intermediate 8 steps
rust
use serde::{Deserialize, Serialize};
 
#[derive(Debug, Serialize, Deserialize)]
pub struct UserProfile {

Trimming JSON output with serde attributes

serialization json attributes
Intermediate 8 steps
go
package middleware
 
import (
	"context"

A deadline-enforcing HTTP middleware in Go

middleware context goroutines
Advanced 7 steps
javascript
import { useEffect, useRef } from "react";
import { useLocation } from "react-router-dom";
 
export function RouteFocus({ title, children }) {

Managing focus on route change in React

accessibility focus-management routing
Intermediate 6 steps
python
import base64
import hashlib
import hmac
import os

How TOTP two-factor codes work

totp hmac authentication
Intermediate 7 steps
java
public final class MultipartParser {
 
    public record FilePart(String name, String filename, String contentType, byte[] content) {}
 

Parsing multipart form data by hand in Java

parsing byte-arrays http
Advanced 8 steps
php
<?php
 
namespace App\Jobs;
 

A retrying queue job in Laravel

queues background-jobs retries
Intermediate 7 steps
ruby
class Contact < ApplicationRecord
  PHONE_FORMATS = {
    "US" => /\A\+1\d{10}\z/,
    "GB" => /\A\+44\d{10}\z/,

Country-aware phone validation in Rails

validation callbacks regex
Intermediate 6 steps
python
from configparser import ConfigParser, ExtendedInterpolation
from pathlib import Path
 
 

Layered INI config loading in Python

configuration parsing defaults
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::State,
    http::{header, StatusCode},

Streaming NDJSON from Postgres in Axum

streaming backpressure async
Advanced 9 steps
java
@AutoConfiguration
@EnableConfigurationProperties(RateLimiterProperties.class)
@ConditionalOnClass(RateLimiter.class)
public class RateLimiterAutoConfiguration {

Building a Spring Boot rate-limiter auto-config

auto-configuration conditional-beans rate-limiting
Intermediate 7 steps
javascript
const STORAGE_KEY = "theme-preference";
 
function getSystemTheme() {
  return window.matchMedia("(prefers-color-scheme: dark)").matches

Building a dark-mode toggle that respects the OS

dark-mode localstorage matchmedia
Intermediate 8 steps
ruby
module AppConfig
  module_function
 
  def fetch(key, default: nil, required: false)

Typed environment variable config in Ruby

environment-variables type-coercion configuration
Intermediate 7 steps
php
final class Container
{
    private array $bindings = [];
    private array $instances = [];

Building a DI container with reflection in PHP

dependency-injection reflection autowiring
Advanced 9 steps