Code Explainers

Browse the library

python
class FileManager:
    """A context manager that safely opens and closes a file."""
 
    def __init__(self, path, mode="r"):

Writing context managers in Python

context-managers resource-management exception-handling
Intermediate 6 steps
java
public class UnionFind {
    private final int[] parent;
    private final int[] rank;
    private int count;

How union-find with path compression works

disjoint-set path-compression union-by-rank
Intermediate 7 steps
go
package calc
 
import "testing"
 

Table-driven tests in Go

table-driven-tests error-handling subtests
Intermediate 7 steps
javascript
class Stack {
  #items = [];
 
  push(value) {

Building a Stack with private fields

data-structures encapsulation iterators
Intermediate 7 steps
typescript
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,

How a NestJS logging interceptor works

interceptors rxjs logging
Intermediate 5 steps
ruby
# Curry a multi-argument lambda so it can be applied one argument at a time.
add = ->(a, b, c) { a + b + c }
 
# Proc#curry returns a curried version that collects arguments incrementally.

Currying lambdas and methods in Ruby

currying closures partial-application
Intermediate 7 steps
php
<?php
 
class Entity
{

PHP magic methods for dynamic attributes

magic-methods overloading encapsulation
Intermediate 6 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
python
from collections import deque
 
 
def bfs(graph, start):

Breadth-first search and shortest paths in Python

graph-traversal bfs queue
Intermediate 9 steps
go
package middleware
 
import (
	"net/http"

Building a bearer-token auth middleware in Gin

middleware authentication closures
Intermediate 5 steps
javascript
function depthFirstSearch(graph, start) {
  const visited = new Set();
  const order = [];
 

Depth-first search and pathfinding in JS

graphs recursion depth-first-search
Intermediate 8 steps
typescript
// A module-scoped Singleton: the single instance lives in this module's
// closure and is never exposed directly.
 
interface AppConfig {

A module-scoped Singleton in TypeScript

singleton generics encapsulation
Intermediate 7 steps
ruby
class ThreadSafeCounter
  def initialize(initial = 0)
    @count = initial
    @mutex = Mutex.new

A thread-safe counter with Mutex in Ruby

concurrency mutex thread-safety
Intermediate 6 steps
php
<?php
 
namespace App\Models;
 

Accessors, mutators & casts in a Laravel model

eloquent accessors mutators
Intermediate 6 steps
python
class Typed:
    """A data descriptor that enforces a type and optional validation."""
 
    def __init__(self, expected_type, validator=None):

Building a typed descriptor in Python

descriptors validation metaprogramming
Advanced 8 steps
java
import java.util.*;
 
public class TopologicalSort {
 

Topological sort with Kahn's algorithm

graphs topological-sort bfs
Intermediate 6 steps
go
package handlers
 
import (
	"net/http"

Reading path parameters in Gin

routing path-parameters wildcards
Beginner 7 steps
javascript
// Sliding window maximum using a monotonic decreasing deque.
// Returns an array of the maximum value within each window of size k.
function maxSlidingWindow(nums, k) {
  const result = [];

Sliding window maximum with a deque

sliding-window monotonic-deque amortized-analysis
Advanced 7 steps