Code Explainers

Code explainers tagged #closures

go
package fsutil
 
import (
	"io/fs"

Walking a directory tree to find large files in Go

filesystem recursion closures
Intermediate 7 steps
python
import threading
from functools import wraps
 
 

A thread-safe debounce decorator in Python

decorators debounce threading
Advanced 6 steps
php
<?php
 
namespace App\Services;
 

Tagged cache invalidation in Laravel

caching cache-tags invalidation
Intermediate 5 steps
go
package middleware
 
import (
	"log"

Per-IP rate limiting middleware in Gin

rate-limiting middleware concurrency
Intermediate 8 steps
javascript
const crypto = require('crypto');
 
function inMemoryCache({ maxAge = 60_000, maxEntries = 500 } = {}) {
  const store = new Map();

Building an in-memory cache middleware in Express

caching middleware etag
Advanced 10 steps
php
namespace App\Providers;
 
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

Adding a whereActive macro in Laravel

macros service-provider query-builder
Intermediate 5 steps
go
package proxy
 
import (
	"log"

Building a hardened Go reverse proxy

reverse-proxy http timeouts
Intermediate 7 steps
typescript
type ResizeCallback = (size: { width: number; height: number }) => void;
 
export function observeResize(callback: ResizeCallback, delay = 150) {
  let timeoutId: ReturnType<typeof setTimeout> | undefined;

Debouncing window resize in TypeScript

debounce closures event-listeners
Intermediate 6 steps
rust
use axum::{
    extract::State,
    routing::{get, post, MethodRouter},
    Json, Router,

Self-documenting routes in Axum

builder-pattern closures shared-state
Intermediate 8 steps
javascript
function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn.apply(this, args);

Building a curry function in JavaScript

currying closures higher-order-functions
Intermediate 7 steps
go
package handlers
 
import (
	"net/http"

Header-driven API versioning in Gin

api-versioning header-binding closures
Intermediate 6 steps
php
<?php
 
namespace App\Providers;
 

Defining rate limiters in Laravel

rate limiting service provider closures
Intermediate 7 steps
go
package api
 
import (
	"encoding/json"

Building reusable JSON helpers in Go HTTP handlers

http json error-handling
Intermediate 9 steps
go
package middleware
 
import (
	"net/http"

Building a CORS middleware in Gin

cors middleware closures
Intermediate 9 steps
rust
use std::fs;
use std::path::Path;
 
use regex::Regex;

Redacting emails in a file with Rust

regex file-io closures
Intermediate 6 steps
go
package middleware
 
import (
	"net/http"

How a www-to-apex redirect middleware works in Gin

middleware http-redirect closures
Intermediate 5 steps
javascript
function memoize(fn, resolver) {
  const cache = new Map();
 
  function memoized(...args) {

Building a memoize wrapper in JavaScript

memoization closures higher-order-functions
Intermediate 7 steps
python
from functools import wraps
 
from django.core.cache import cache
from django.http import JsonResponse

Building a rate-limit decorator in Django

decorators rate limiting caching
Intermediate 7 steps