Code Explainers

Code explainers tagged #higher-order-functions

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