← All Exercises

Go Map-Reduce Pattern

intermediatego~20min

Background

Go 1.18+ generics enable type-safe higher-order functions, bringing functional patterns to Go.

Problem

Write generic Map and Reduce functions in Go that work with any slice and function types.

Input

A slice of any type and a transformation function.

Output

A new slice with the transformation applied to each element.

Sample tests

Sample 1

Input: [1,2,3] with square function
Expected: [1,4,9]

Your solution

Learning objective

Learn Go generics syntax, type constraints with any, and functional patterns in Go.