Convert imperative loops into a readable functional pipeline
Nested for-loops with accumulator variables are error-prone and hard to review. This prompt converts them to a functional style step-by-step while preserving exact behavior and flagging cases where the rewrite would hurt performance.
You are a senior developer who values readable, functional-style code. I want to refactor imperative loop code into a cleaner pipeline.
Language: {{LANGUAGE}}
Code to refactor:
```
{{LOOP_CODE}}
```
Performance constraints (e.g., 'runs on 10M rows', 'latency-sensitive hot path', or 'none known'):
{{PERFORMANCE_CONSTRAINTS}}
Follow these steps:
1. Describe what the existing code computes in one plain-English sentence. Confirm before proceeding.
2. Identify each logical stage of the loop: filtering, transforming, grouping, accumulating, sorting, etc.
3. Map each stage to a functional equivalent (map, filter, reduce, flatMap, groupBy, etc.) available in {{LANGUAGE}}.
4. Write the refactored version as a pipeline. Preserve the exact output type and semantics — if the original mutates external state, flag that explicitly rather than silently dropping it.
5. Add a brief inline comment on any step that is not immediately obvious.
6. Evaluate the performance impact: does the functional version create extra intermediate collections or iterate more times? If so, suggest whether lazy evaluation or a single-pass approach is warranted given the stated constraints.
7. If the original loop cannot be cleanly expressed in functional style (e.g., early-exit logic, complex interdependent state), say so and suggest a partial refactor instead. {{LANGUAGE}}{{LOOP_CODE}}{{PERFORMANCE_CONSTRAINTS}}
How to use this prompt
- Copy the prompt above (Copy button on the top-right).
- Replace each
{{VAR}}with your own value. Variables:{{LANGUAGE}}{{LOOP_CODE}}{{PERFORMANCE_CONSTRAINTS}}. - Paste it into one of the recommended tools below.
- Iterate: tighten constraints in the prompt if the output is generic.
Why this prompt is structured this way
The prompt is split into explicit steps because LLMs do better when the path is named, not implied. Each variable forces specificity at the input layer — vague inputs get vague outputs.
Pair this prompt with a tool
Cursor
$20/moAI-first code editor forked from VS Code.
Cursor is VS Code with first-class LLM integration. Multi-file edits, codebase Q&A, autocomplete that's actually useful. The default IDE for many shipping engineers in 2026.
Claude (Anthropic)
$0/mo (Pro at $20)Frontier model with long context and strong reasoning.
Claude (Opus / Sonnet / Haiku tiers) is the assistant favored by writers and engineers who care about reasoning quality and tone. 1M token context on Opus.
ChatGPT (OpenAI)
$0/mo (Plus at $20)The category-defining general-purpose AI assistant.
ChatGPT has the broadest feature surface: image gen, voice, custom GPTs, web browsing, code execution. Often the right default; sometimes beaten on specific tasks by Claude or Perplexity.
The PlaybookPrompts weekly
One short email per week. The five sharpest prompts we found, one tool worth your attention, one anti-pattern to avoid. Unsubscribe in one click.
Newsletter signup is not configured. Set PUBLIC_NEWSLETTER_USERNAME in the build env.