Tags

Functional Programming

Functional Programming

0 lessons
8 code snippets
4 community items

functional-programming

Code Snippets

8 snippets
Code Snippet

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.

JavaScript
arrays
utility
array-manipulation-patterns
functional-programming

758

18

Medium
Code Snippet

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.

JavaScript
arrays
utility
array-manipulation-patterns
functional-programming

1k

6

Medium
Code Snippet

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.

JavaScript
utility
code-template
functional-programming

813

11

Easy
Code Snippet

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.

JavaScript
utility
code-template
currying
functional-programming

699

3

Medium
Code Snippet

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.

JavaScript
utility
code-template
composition
functional-programming

954

16

Medium
Code Snippet

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.

C++
cpp-lambdas
functional-programming
cpp-stl

212

3

Easy
Code Snippet

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.

Ruby
ruby-blocks
ruby-yield
ruby-procs-lambdas
functional-programming

799

4

Medium
Code Snippet

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.

PHP
php-array-functions
map-filter-reduce
functional-programming

516

6

Easy