Examples

Page 46 of 50

Browse code examples

javascriptSnippet

Run A Function Only Once

Wrap sync and async functions so initialization runs at most once, sharing the cached result or in-flight promise.

javascriptSnippet

Sliding Window Maximum

Compute the maximum of every fixed-size window in O(n) using a monotonic deque, with clear handling of invalid sizes.

pythonSnippet

Asyncio Queue Worker Pool

Fan out jobs to a bounded asyncio queue, run a fixed worker pool, and shut down cleanly with sentinel values and task_done.

pythonSnippet

Injectable Clock For Tests

Abstract time behind a Clock protocol, provide a fake clock that can advance instantly, and drive a TTL cache without sleeping.