Filter And Map In Scala

mapFunction takes the integer input from command line. A list must be generated with the input number of elements in the list starting from zero. Each number in the List generated numberList must be incremented by 5 using the map and incrementer function, and the resultant list must be printed.

object mapFunction{
def main(args :Array[String]){
val input = args(0).toInt
val numberList = //complete the code
def incrementer //complete the code
val incrementList = //complete the code
println(incrementList)
}
}

output will be : List(5,6,7)

I did it :

object mapFunction{
def main(args :Array[String]){
val input = args(0).toInt
val numberList = List.range(0, input+1)
def incrementer //complete the code
val incrementList = //complete the code
println(incrementList)
}
}

then how to proceed, I am a new learner. please help

In your code snippet incrementer is already given as a def, so you should make it a method, that takes an integer and adds 5 to it.
Then have a look at the documentation for the List.map method

If you still can’t solve it, I’d strongly recommend reading a tutorial on Scala first (e.g. the freely available Scala Book).

2 Likes

Hi! All Can u plz provide me the solution for this! Even I wanted to know on for the lab exercise which was givwen! Kindly provide me the solution!

@sourav311 Where are you getting stuck when you attempt to solve it yourself? We’re happy to help, but we don’t just give out solutions to exercises.

1 Like