Code Explainers

Intermediate code explainers

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
java
import java.util.*;
 
public class TopologicalSort {
 

Topological sort with Kahn's algorithm

graphs topological-sort bfs
Intermediate 6 steps
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
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
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