# 035: Reduce # Combine all elements into a single value numbers = [1, 2, 3, 4, 5] # Sum all numbers total = numbers.reduce((sum, num) => sum + num, 0) show total # 15 # Product of all numbers product = numbers.reduce((prod, num) => prod * num, 1) show product # 120 # Find maximum max = numbers.reduce((max_so_far, num) => { if num > max_so_far: num else: max_so_far }, numbers[0]) show max # 5 # Build object from list items = ["apple", "banana", "cherry"] counts = items.reduce((obj, item) => { obj[item] = item.length obj }, {}) show counts # { apple: 5, banana: 6, cherry: 6 }