Code Explainers

Intermediate code explainers

python
from difflib import SequenceMatcher
from bisect import bisect_left, bisect_right
 
 

Building a fuzzy autocomplete matcher

fuzzy-matching binary-search ranking
Intermediate 9 steps
rust
use axum::{
    body::Body,
    http::{header, HeaderValue, StatusCode, Uri},
    response::{IntoResponse, Response},

Serving embedded static files in Axum

static-assets embedding http-headers
Intermediate 7 steps
php
<?php
 
namespace App\Queue;
 

A stable priority job queue in PHP

priority-queue data-structures closures
Intermediate 8 steps
javascript
function deepFreeze(obj) {
  const propNames = Reflect.ownKeys(obj);
 
  for (const name of propNames) {

Recursively freezing a nested object

recursion immutability object-freezing
Intermediate 6 steps
go
package handlers
 
import (
	"net/http"

Custom query validation in Gin

validation query-binding deduplication
Intermediate 8 steps
typescript
import { Controller, Get, Param, Query, Redirect, NotFoundException } from '@nestjs/common';
import { LinksService } from './links.service';
 
@Controller('links')

Handling HTTP redirects in NestJS

redirects routing decorators
Intermediate 7 steps
python
import secrets
from datetime import datetime, timedelta, timezone
 
from fastapi import APIRouter, Cookie, Depends, HTTPException, Response, status

Cookie session auth in FastAPI

session-authentication cookies dependency-injection
Intermediate 8 steps
java
public class DuplicateFinder {
 
    public Map<String, List<Path>> findDuplicates(Path root) throws IOException {
        Map<String, List<Path>> byHash = new HashMap<>();

Finding duplicate files by content hash

hashing file-io streams
Intermediate 8 steps
php
<?php
 
namespace App\Support;
 

Normalizing phone numbers to E.164 in PHP

input-validation normalization regular-expressions
Intermediate 8 steps
javascript
class InfiniteScroll {
  constructor(sentinel, { loadMore, root = null, rootMargin = '200px' } = {}) {
    this.sentinel = sentinel;
    this.loadMore = loadMore;

Infinite scroll with IntersectionObserver

intersectionobserver pagination async
Intermediate 10 steps
go
package storage
 
import (
	"context"

A SQLite-backed order store in Go

database/sql sqlite error-wrapping
Intermediate 7 steps
php
final class Route
{
    private string $regex;
    private array $paramNames = [];

Compiling route patterns into regex in PHP

routing regex parsing
Intermediate 8 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

How guards chain auth in NestJS

guards authentication authorization
Intermediate 8 steps
python
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from sqlalchemy.orm import Session

Chaining auth dependencies in FastAPI

dependency-injection jwt-authentication authorization
Intermediate 8 steps
ruby
require "net/http"
require "json"
 
class UrlFetcher

A thread-pool URL fetcher in Ruby

concurrency thread-pool queues
Intermediate 8 steps
rust
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
 

A round-robin worker pool in Rust

concurrency thread-pool channels
Intermediate 7 steps
go
package auth
 
import (
	"errors"

Hashing and verifying passwords with bcrypt in Go

password-hashing bcrypt error-handling
Intermediate 7 steps
php
public function customerTotals(Request $request): Collection
{
    return DB::table('orders')
        ->join('customers', 'customers.id', '=', 'orders.customer_id')

Building an aggregate report with Laravel's query builder

query builder aggregation conditional queries
Intermediate 8 steps