PocketFlow / design_pattern
The-Pocket/PocketFlow/.cursor/rules/design_pattern/mapreduce.mdc
Guidelines for using PocketFlow, Design Pattern, Map Reduce
Cursor rule11k starsChanged 15 months ago
What's in it
- Map Reduce
- Example: Document Summarization
---
description: Guidelines for using PocketFlow, Design Pattern, Map Reduce
globs:
alwaysApply: false
---
# Map Reduce
MapReduce is a design pattern suitable when you have either:
- Large input data (e.g., multiple files to process), or
- Large output data (e.g., multiple forms to fill)
and there is a logical way to break the task into smaller, ideally independent parts.
You first break down the task using [BatchNode](../core_abstraction/batch.md) in the map phase, followed by aggregation in the reduce phase.
### Example: Document Summarization
```python
class SummarizeAllFiles(BatchNode):
def prep(self, shared):
files_dict = shared["files"] # e.g. 10 files
return list(files_dict.items()) # [("file1.txt", "aaa..."), ("file2.txt", "bbb..."), ...]
def exec(self, one_file):
filename, file_content = one_file
summary_text = call_llm(f"Summarize the following file:\n{file_content}")
return (filename, summary_text)
def post(self, shared, prep_res, exec_res_list):
shared["file_summaries"] = dict(exec_res_list)
class CombineSummaries(Node):
def prep(self, shared):
return shared["file_summaries"]
def exec(self, file_summaries):
# format as: "File1: summary\nFile2: summary...\n"
text_list = []
for fname, summ in file_summaries.items():
text_list.append(f"{fname} summary:\n{summ}\n")
big_text = "\n---\n".join(text_list)
return call_llm(f"Combine these file summaries into one final summary:\n{big_text}")
def post(self, shared, prep_res, final_summary):
shared["all_files_summary"] = final_summary
batch_node = SummarizeAllFiles()
combine_node = CombineSummaries()
batch_node >> combine_node
flow = Flow(start=batch_node)
shared = {
"files": {
"file1.txt": "Alice was beginning to get very tired of sitting by her sister...",
"file2.txt": "Some other interesting text ...",
# ...
}
}
flow.run(shared)
print("Individual Summaries:", shared["file_summaries"])
print("\nFinal Summary:\n", shared["all_files_summary"])
```
> **Performance Tip**: The example above works sequentially. You can speed up the map phase by running it in parallel. See [(Advanced) Parallel](../core_abstraction/parallel.md) for more details.
{: .note }More agent context in The-Pocket/PocketFlow
19 other files this repository gives its agents.
Cursor rule
- .cursor/rules/core_abstraction/async.mdc
- .cursor/rules/core_abstraction/batch.mdc
- .cursor/rules/core_abstraction/communication.mdc
- .cursor/rules/core_abstraction/flow.mdc
- .cursor/rules/core_abstraction/node.mdc
- .cursor/rules/core_abstraction/parallel.mdc
- .cursor/rules/design_pattern/agent.mdc
- .cursor/rules/design_pattern/multi_agent.mdc
- .cursor/rules/design_pattern/rag.mdc
- .cursor/rules/design_pattern/structure.mdc
- .cursor/rules/design_pattern/workflow.mdc
- .cursor/rules/guide_for_pocketflow.mdc
- .cursor/rules/utility_function/chunking.mdc
- .cursor/rules/utility_function/embedding.mdc
- .cursor/rules/utility_function/llm.mdc
- .cursor/rules/utility_function/text_to_speech.mdc
- .cursor/rules/utility_function/vector.mdc
- .cursor/rules/utility_function/viz.mdc
- .cursor/rules/utility_function/websearch.mdc
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
Reports can't be read right now.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

