Help for the VSCode editor.
-
Select the correct statement(s) for high order functions
- Function that receives a function as an argument.
- Function that contains a function.
- Function that returns another function.
- Function that calls another function.
Reveal
A. C
These two answers correctly define a high order function.
-
What would be the output of the following program?
package main import "fmt" func addHundred(x int) int { return x + 100 } func partialSum(x ...int) func() { sum := 0 for _, value := range x { sum += value } return func() { fmt.Println(addHundred(sum)) } } func main() { partial := partialSum(1, 2, 3, 4, 5) partial() }
- 110
- 15
- Error
- 115
Reveal
115
- The function
partialSum
adds together the values of the integers it receives and stores them insum
. - It then returns an anonymous function that prints the value of
sum
plus 100 (via call toaddHundred
). - Recall that the body of this anonymous function is an inner scope with respect to
partialSum
meaning it can use the variablesum
. - The variable
partial
is assigned the result of the call topartialSum
. Its value is the anonymous function returned bypartialSum
. - Finally the anonymous function is executed via the variable
partial
by applying the call operator()
- i.e.partial()
and the result is printed.
-
What would be the output of the following program?
package main func addHundred(x int) int { return x + 100 } func partialSum(x ...int) func() int { sum := 0 for _, value := range x { sum += value } return func() int { return addHundred(sum) } } func main() { partial := partialSum(1, 2, 3) partial() }
- 106
- no output
- Error
- 115
Reveal
No output
Quite simply there is no
fmt.Println
so nothing will be printed.This is a minor variation on the previous question. The caculated result would indeed be
106
, but since thefmt.Println
has been removed from the returned anonymous function, there is no output. -
What would be the output of the following program?
package main import "fmt" func addHundred(x int) int { return x + 100 } func partialSum(add100 func(x int) int, x ...int) int { sum := 0 for _, value := range x { sum += value } return add100(sum) } func main() { partial := partialSum(addHundred, 1, 2, 3) fmt.Println(partial) }
- 6
- 115
- 106
- error
Reveal
106
Yet another variation on the theme, however this time...
- The
addHundred
function is being passed as the first argument topartialSum
, then the list of integers to sum. partialSum
sums the integers, then calls theaddHundred
function and returns its result.main()
assigns that value topartial
(whose type will now beint
), and prints it.
-
What would be the output of the following program?
package main import "fmt" func addHundred(x int) { fmt.Println(x + 100) } func partialSum(add100 func(x int), x ...int) int { sum := 0 for _, value := range x { sum += value } add100(sum) return 0 } func main() { partial := partialSum(addHundred, 1, 2, 3) fmt.Println(partial) }
- Error
- 106
0 - 115
1 - 115
0
Reveal
106
0Similar to the previous question, however this time...
addHundred
does the printing of the result of the sum.partialSum
simply returns zeromain()
assigns that value topartial
(whose type will now beint
), and prints it (zero).