Code Explainers

Code explainers tagged #recursion

python
from collections.abc import Mapping
from typing import Any, Iterator
 
 

Flattening nested config into dotted keys

recursion generators tree-traversal
Intermediate 7 steps
ruby
require 'json'
require 'set'
 
class SensitiveScrubber

Recursively scrubbing secrets from JSON

recursion data-masking pattern-matching
Intermediate 7 steps
python
from pathlib import Path
 
 
def print_tree(root, prefix="", show_hidden=False):

Printing a directory tree with recursion

recursion filesystem sorting
Intermediate 6 steps
python
from dataclasses import dataclass, fields, MISSING
from typing import get_type_hints, get_origin, get_args, Union
 
 

Type-checking dataclasses at runtime

dataclasses type-hints runtime-validation
Advanced 8 steps
typescript
type CloneInput = Record<string, unknown> | unknown[] | Map<unknown, unknown> | Set<unknown>;
 
export function deepClone<T>(value: T): T {
  if (typeof structuredClone === "function") {

Building a deepClone with cycle safety

recursion deep-copy cycle-detection
Intermediate 8 steps
python
from functools import wraps
 
 
def memoize(func):

Building a memoize decorator in Python

decorators closures memoization
Intermediate 5 steps
ruby
module Fibonacci
  CACHE = Hash.new do |cache, n|
    cache[n] = cache[n - 1] + cache[n - 2]
  end

Memoizing Fibonacci with a Hash default block

memoization recursion hash-default
Intermediate 5 steps
java
public final class BusinessDays {
 
    private BusinessDays() {
    }

Counting business days between two dates in Java

date arithmetic utility class streams
Intermediate 7 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
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
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
java
public final class Quicksort {
 
    private Quicksort() {
    }

Quicksort with Lomuto partitioning in Java

recursion divide-and-conquer in-place
Intermediate 7 steps