Functional Programming
functional-programming
Code Snippets
Partition an Array by a Predicate
Calling `array.filter(p)` and `array.filter((x) => !p(x))` works but walks the input twice and runs the predicate twice per element, which is wasteful and (for non-pure predicates) plain wrong. A single-pass `partition` returns the matched and unmatched buckets in one go. This snippet covers a clean fold-based implementation, an N-way `partitionBy` for multi-class splits, and a streaming variant that lazily partitions an iterable without materialising the full input.
Zip Multiple Arrays
Python users miss `zip(a, b)` and the Lodash crowd reaches for `_.zip`, but JavaScript can do this cleanly with one helper. This snippet covers the basic two-array zip, an N-array variadic version, the unzip inverse, and a `zipWith` that lets you fold pairs into custom shapes (records, objects, weighted sums). It also clarifies the truncate-to-shortest vs. fill-with-undefined trade-off.
Map Over an Object's Values
JavaScript objects have no built-in `mapValues` like Ramda or Underscore, so every codebase eventually grows its own. This snippet covers the canonical `Object.fromEntries` transform, a sibling `mapKeys` for renaming, and a combined version that lets you reshape both keys and values in one pass. Use them to coerce types, format display strings, or normalise API payloads in a few lines.
Curry a Function in JavaScript
Currying turns a multi-argument function into a chain of single-argument calls so you can pre-bind some arguments and pass the rest later. This snippet covers the variadic curry every codebase reaches for, a placeholder-aware version for skipping argument positions, and a typed-friendly fixed-arity helper for stricter call shapes. Use it for partial application and to build small DSLs.
Compose and Pipe Helpers
Composing small functions into a single transformation is the bread and butter of functional pipelines. This snippet contrasts the right-to-left `compose` (math notation) with the left-to-right `pipe` (data-flow notation), then shows an async-aware `pipeAsync` for chained `await`-able steps. Use them to flatten nested calls into a readable left-to-right (or right-to-left) sequence.
C++ Lambda Basics
Lambdas (C++11+) are first-class anonymous function objects with a compact syntax: `[capture](params) { body }`. This snippet covers the basic form, the difference between by-value `[=]` and by-reference `[&]` captures, and using lambdas with standard algorithms like `std::sort` and `std::for_each`. Reach for them anywhere you would write a small functor or pass a callback.
Blocks, Procs, and Lambdas
Ruby has three closely related callable forms: blocks (one-shot, attached to a method call), `Proc` objects (named, non-strict about arity), and `Lambda` objects (named, strict about arity and `return` semantics). This snippet shows how each is constructed, when `yield` versus `&block` is appropriate, and the subtle differences in `return` and arity checking that decide which to use.
PHP Array Functions Cheat Sheet
PHP's array library is dense; once you know `array_map`, `array_filter`, `array_reduce`, and a few of their cousins, most array work becomes one-liners. This snippet covers the trio plus the surprising key-preserving behaviour of `array_filter` and the keys-to-values pivot via `array_combine`. Reach for these instead of writing manual `foreach` loops.
Community
Python Decorators Explained with Five Real Examples
Decorators stop being magic the moment you see five real ones in a row: timing, caching, auth, retry, and rate limiting. Here is the pattern, the gotchas, and the line where I stop reaching for them.
An argv Parser Without yargs or commander
Eighty percent of the time I just need --flag and --opt=value parsing for a one-off script. Adding yargs feels heavy. This is the 30-line zero-dependency parser I copy into every Node script.
Functional Core, Imperative Shell, Explained
The architecture pattern that gives you most of functional programming's testability without a rewrite. The two-layer rule, where each layer's responsibilities sit, and the failure modes of mixing them.
Pure Functions, Immutability, and the Trade-offs
Pure functions and immutability are not free. The honest accounting of the wins (testability, reasoning, parallelism) and the costs (allocation, ergonomics, language fit), and where the line falls in real code.
