Examples

Page 25 of 50

Browse code examples

javaSnippet

Optional ifPresentOrElse Branch

Handle both branches of an Optional in one call with ifPresentOrElse: the value action and the empty action live side by side, no if-get wrapping.

javaSnippet

Optional Or Fallback Chain

Build lazy fallbacks with Optional.or: each supplier runs only when the previous chain stage is empty, unlike eager orElse arguments.

javaSnippet

WeakHashMap Key Life Cycle

Store metadata beside objects with WeakHashMap: entries vanish once keys are garbage collected, so caches do not leak strong references.

javascriptSnippet

AbortSignal Timeout Ends Slow Work

AbortSignal.timeout builds a deadline signal that aborts itself with a TimeoutError; wire it into slow work with throwIfAborted and one abort listener.

javascriptSnippet

padEnd Aligns Report Columns

padEnd left-aligns names while padStart right-aligns numbers, producing fixed-width report columns; over-wide values are never truncated.

javascriptSnippet

Reduce Folds Records Into A Map

Fold an array into a Map of per-key aggregates in one pass: the Map keeps first-seen order, and the object-spread version exposes a per-iteration copy trap.

pythonSnippet

Dict Keys Give Stable Dedupe

Deduplicate records by choosing a key function and relying on dict insertion order: first-wins and last-wins are both one pass with no aux dedupe type.

sqlSnippet

Except Snapshot Row Diff

Diff two row snapshots with EXCEPT in both directions: rows only in the old load are removals, rows only in the new load are additions.

sqlSnippet

Greatest Least Clamp Values

Clamp numbers into bounds with GREATEST and LEAST: chain both for a range clamp; PostgreSQL ignores NULL inputs, returning NULL only when all are NULL.