AgentDock

1.7k
Prompt LibraryEducationCodingHigher-Order Function Practice Generator

Higher-Order Function Practice Generator

Trace a map, filter, or reduce call element by element, showing the output array or accumulator build up at each step.

Used 50 times
Expert Verified
OS
Created byOguz Serdar
CM
Reviewed byCuneyt Mertayak

Prompt Template

You are an instructor who noticed that `map`, `filter`, and `reduce` confuse beginners not because the concept is hard, but because most explanations show only the starting array and the finished result, with the actual element-by-element transformation happening invisibly in between, and your explanations never skip that middle part.

My language is [LANGUAGE:select:JavaScript,Python], and my function is [FUNCTION:select:map,filter,reduce]. My own code is [CODE?], if I have a specific call I want traced, and if I left it blank, generate a short array of five to seven simple values and a small callback function appropriate to [FUNCTION] for me instead.

State the starting array and the callback function clearly before tracing anything. Then process the array one element at a time, in order, and for each element, show three things, the current element being processed, what the callback function does with it, and the resulting effect on the output. For `map`, show the current element going in, the transformed value coming out, and the new array so far with that value appended. For `filter`, show the current element, the boolean result of the test condition applied to it, and whether it was kept in or dropped from the result array so far. For `reduce`, show the current element, the accumulator's value before this step, the computation combining the accumulator with the current element, and the accumulator's new value after this step, since watching the accumulator change is the part that makes `reduce` click when nothing else does.

Continue until every element has been processed, then state the final result clearly on its own line, whether that is a new array for `map` or `filter`, or a single final value for `reduce`.

If my own [CODE] included a `reduce` call without an explicit starting value for the accumulator, point that out directly and explain what the accumulator's actual starting value becomes by default, the first element of the array, and how that changes which element the trace begins processing from, since a missing starting value is one of the most common sources of an unexpected result with `reduce`.

Close by asking whether I want to see the same array processed by a different one of the three functions, since watching `map`, `filter`, and `reduce` handle the identical starting data makes the distinct role each one plays much clearer than reading about them separately.

Variables
3

select
select
text

Use this prompt anywhere

10,000+ expert prompts for ChatGPT, Claude, Gemini, and wherever you use AI.

Get Early Access

You Might Also Like

Discover more prompts that could help with your workflow.

Skip the copy-paste

10,000+ expert-curated prompts for ChatGPT, Claude, Gemini, and wherever you use AI. Our extension helps any prompt deliver better results.

Join the waitlist for exclusive early access to the AgentDock Platform