Hone logo
Hone
Problems

Go Higher-Order Function: The Function Factory

Higher-order functions are functions that can take other functions as arguments or return functions as results. This powerful concept is fundamental to functional programming and allows for more abstract, reusable, and expressive code. This challenge will guide you in creating a higher-order function in Go that generates other functions with specific behavior.

Problem Description

Your task is to create a higher-order function in Go called CreateMultiplier. This function should accept an integer multiplier as an argument and return another function. The returned function should accept a single integer value and return the result of multiplying value by the multiplier that was originally passed to CreateMultiplier.

Key Requirements:

  • Define a function CreateMultiplier that takes an int named multiplier.
  • CreateMultiplier must return a function.
  • The returned function must take an int named value.
  • The returned function must return an int which is value * multiplier.

Expected Behavior:

When you call CreateMultiplier with a specific number, it "remembers" that number and creates a function tailored to multiply by it.

Examples

Example 1:

Input:
multiplier := 5
doubler := CreateMultiplier(multiplier)
result := doubler(10)

Output:
50

Explanation:
`CreateMultiplier(5)` returns a function that multiplies its input by 5.
When this returned function is called with 10, it calculates 10 * 5, resulting in 50.

Example 2:

Input:
halfMultiplier := CreateMultiplier(2)
thirdMultiplier := CreateMultiplier(3)

result1 := halfMultiplier(10)
result2 := thirdMultiplier(10)

Output:
20
30

Explanation:
`CreateMultiplier(2)` creates a function that multiplies by 2.
`CreateMultiplier(3)` creates a function that multiplies by 3.
Calling the first with 10 yields 20. Calling the second with 10 yields 30.

Example 3:

Input:
zeroMultiplier := CreateMultiplier(0)
negativeMultiplier := CreateMultiplier(-3)

result1 := zeroMultiplier(100)
result2 := negativeMultiplier(4)

Output:
0
-12

Explanation:
`CreateMultiplier(0)` returns a function that always returns 0, regardless of input.
`CreateMultiplier(-3)` returns a function that multiplies by -3.
Calling the first with 100 results in 0. Calling the second with 4 results in -12.

Constraints

  • The multiplier argument passed to CreateMultiplier will be an integer.
  • The value argument passed to the returned function will be an integer.
  • The intermediate and final results of the multiplication will fit within a standard Go int type.
  • Your solution should be efficient and idiomatic Go.

Notes

This challenge is designed to help you understand closures and how functions can "capture" variables from their surrounding scope. Think about how the multiplier value needs to be accessible by the function that CreateMultiplier returns, even after CreateMultiplier itself has finished executing. You do not need to worry about error handling for this challenge.

Loading editor...
go