Code Explainers
Code explainers tagged #higher-order-functions
ruby
class Pipeline def initialize @middlewares = [] end
Building a middleware pipeline in Ruby
closures
middleware
composition
Advanced
8 steps
typescript
type AsyncMethod = (...args: any[]) => Promise<any>; function LogExecutionTime(thresholdMs = 0) { return function <T extends AsyncMethod>(
A method decorator that times async calls
decorators
higher-order-functions
async
Advanced
9 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
typescript
type SortDirection = "asc" | "desc"; interface SortKey<T> { selector: (row: T) => string | number | Date | null | undefined;
Multi-column sorting in TypeScript
generics
comparators
sorting
Intermediate
6 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
javascript
function makeTableSorter(rows) { const state = { key: null, dir: 1 }; const compareBy = (key) => (a, b) => {
Building a stateful table sorter in JavaScript
closures
stable-sort
comparators
Intermediate
7 steps
typescript
import { inject } from '@angular/core'; import { CanActivateFn, Router,
Functional route guards in Angular
route-guards
dependency-injection
observables
Intermediate
5 steps
javascript
const { validationResult, matchedData } = require('express-validator'); function validate(schema) { const runners = schema.map((rule) => rule.run.bind(rule));
A reusable validation middleware in Express
middleware
validation
higher-order-functions
Intermediate
8 steps
javascript
const asyncHandler = (fn) => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next); };
Async error handling in Express routes
async-await
error-handling
middleware
Intermediate
7 steps
php
function memoize(callable $fn): callable { $cache = [];
How memoization works in PHP closures
memoization
closures
higher-order-functions
Intermediate
8 steps
javascript
function groupBy(items, keySelector) { const resolveKey = typeof keySelector === 'function' ? keySelector : (item) => item[keySelector];
Building a flexible groupBy in JavaScript
higher-order-functions
reduce
data-transformation
Intermediate
6 steps
typescript
interface TokenBucketOptions { capacity: number; refillPerSecond: number; }
How a token bucket rate limiter works
rate-limiting
token-bucket
lazy-evaluation
Intermediate
7 steps
python
from collections import OrderedDict from typing import Callable, Hashable, Iterable, Iterator, TypeVar T = TypeVar("T")
Two ways to dedupe while keeping order
deduplication
generators
ordered-data
Intermediate
7 steps
php
<?php namespace App\Support;
Grouping records with array_reduce in PHP
array-reduce
grouping
higher-order-functions
Intermediate
6 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
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
javascript
function throttle(fn, wait) { let lastCall = 0; let timeoutId = null; let lastArgs = null;
Building a leading-and-trailing throttle
closures
rate-limiting
timers
Advanced
6 steps