Create the Map() function

Report a typo

Kendrick wants to create a special generic function that allows him to process and transform all the items in a slice of any type, without using an explicit for loop. This type of function is known as a map function.

Kendrick has already written part of the code for his Map() function, however, he needs your help to finish the implementation of the Map() function and also to make Map() a generic function that can take and return a slice of any type.

Sample Input 1:

Vroom Room Zoom
1 2 3

Sample Output 1:

[VroomVroom RoomRoom ZoomZoom]
[1 4 9]
Write a program in Go
package main

import "fmt"

// Make Map() a generic function that can take 'any' data type
// and return a slice of 'any' data type as well!
func Map[? ?](s []?, modify func(elem T) T) []? {
if s == nil {
return nil
} else if modify == nil {
return s
}

// Help Kendrick by writing the rest of the required code
// to finish the implementation of the Map() function:
mapped := make([]?, len(s))
for i, elem := range s {
mapped[i] = modify(?)
}
return ? // What variable should the Map() function return!?
}

// DO NOT delete or modify the contents within the main function!
func main() {
var str1, str2, str3 string
fmt.Scanln(&str1, &str2, &str3)

var num1, num2, num3 int
fmt.Scanln(&num1, &num2, &num3)

stringSlice := []string{str1, str2, str3}
intSlice := []int{num1, num2, num3}

stringSlice = Map(stringSlice, func(elem string) string {
return elem + elem
})
___

Create a free account to access the full topic