Map, Filter, Reduce
Map, Filter, and Reduce are the three most common collection processing patterns. Here we demonstrate their core logic using explicit loops.
Map applies a transformation function to each element, producing a new collection. For example, squaring each number. Filter selects elements that satisfy a condition. For example, keeping only even numbers. Reduce combines all elements into a single value, typically requiring an initial value and an accumulator function.
Cangjie's standard library provides higher-order function forms of these operations, which will be introduced in later chapters. Here we use explicit loops to make each step's logic clearly visible, suitable for beginners to understand the essence.
The code on the right executes on an integer list: map (square), filter (select even numbers), and reduce (sum).