Code Explainers

Browse the library

java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 

Java try-with-resources and AutoCloseable

try-with-resources autocloseable resource-management
Intermediate 6 steps
typescript
interface RetryOptions {
  retries?: number;
  baseDelayMs?: number;
  maxDelayMs?: number;

Retrying async tasks with exponential backoff

retry exponential-backoff async
Intermediate 8 steps
python
from collections import Counter
 
 
def word_frequencies(text):

Counting things with collections.Counter

counting multiset data-structures
Beginner 8 steps
javascript
function debounce(fn, delay) {
  let timeoutId = null;
 
  function debounced(...args) {

Building a debounce function in JavaScript

closures timers higher-order-functions
Intermediate 6 steps
go
package main
 
import "fmt"
 

How Go slices grow and share memory

slices memory append
Intermediate 7 steps
rust
use std::thread;
 
pub fn parallel_map_reduce<T, M, R, F, G, H>(
    data: &[T],

A parallel map-reduce with scoped threads

concurrency scoped-threads map-reduce
Advanced 8 steps
java
import java.util.Arrays;
 
public class SlidingWindow {
    // Returns the maximum sum of any contiguous subarray of length k.

The sliding window technique in Java

sliding-window arrays algorithms
Intermediate 8 steps
python
def two_sum_sorted(numbers, target):
    """Find indices of two values that sum to target in a sorted array."""
    left, right = 0, len(numbers) - 1
    while left < right:

Two-pointer search on sorted arrays

two-pointers arrays sorting
Intermediate 9 steps
go
package main
 
import "sync"
 

Mutex vs RWMutex in Go

concurrency mutex synchronization
Intermediate 6 steps
ruby
# Binary search using Ruby's built-in Array#bsearch.
#
# There are two modes:
#   1. find-minimum mode  -> block returns true/false (monotonic)

Binary search patterns with Ruby's bsearch

binary-search algorithms ranges
Intermediate 7 steps
typescript
type NestedArray<T> = Array<T | NestedArray<T>>;
 
function flatten<T>(input: NestedArray<T>): T[] {
  const result: T[] = [];

Recursively flattening nested arrays in TypeScript

recursion recursive-types generics
Intermediate 7 steps
java
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

Breadth-first search on an adjacency-list graph

graph-traversal bfs adjacency-list
Intermediate 7 steps
javascript
function flatten(arr) {
  const result = [];
  for (const item of arr) {
    if (Array.isArray(item)) {

Flattening nested arrays with recursion

recursion arrays reduce
Intermediate 7 steps
ruby
class LinkedList
  include Enumerable
 
  Node = Struct.new(:value, :next_node)

Building an enumerable linked list in Ruby

linked-list enumerable data-structures
Intermediate 7 steps
javascript
function getCityName(user) {
  return user?.address?.city ?? "Unknown city";
}
 

Optional chaining and nullish coalescing in JS

optional-chaining nullish-coalescing defensive-coding
Intermediate 5 steps
typescript
class LRUCache<K, V> {
  private readonly capacity: number;
  private readonly map: Map<K, V>;
 

Building an LRU cache on a JS Map

caching data-structures generics
Intermediate 8 steps
ruby
module FileResource
  # Opens a file, yields it to the caller, and guarantees the
  # handle is closed even if the block raises an exception.
  def self.open(path, mode = "r")

Guaranteeing cleanup with begin/ensure in Ruby

resource-management exception-safety blocks
Intermediate 8 steps
rust
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
 

A worker thread driven by Rust channels

concurrency message-passing channels
Intermediate 8 steps