your saying write code as -
object ListMapHigherOrder{
def main(args:Array[String])
{
val intRes = args.toList
val intList: List[Int] = intRes.map(_.toInt).toList
def isPerfectNumber(input: Int) :String =
{
val check_sum = ( (2 to math.sqrt(input).toInt).collect { case x if input % x == 0 => x + input / x} ).sum
if ( check_sum == input - 1 )
return “true”
else
return “false”
}
def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String] = { argVal.map(argFn) }
println(myHigherOrderFunction(isPerfectNumber, intList))
}
}
Final code is
object ListMapHigherOrder{
def main(args:Array[String]) = {
val intList: List[Int] = args.map(_.toInt).toList
def isPerfectNumber(input: Int) :String = {
val check_sum = ( (2 until input).collect { case x if input % x == 0 => x } ).sum
if (input <= 1) "false"
else if ( check_sum == input - 1 ) "true"
else "false"
}
def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String] = { argVal.map(argFn) }
println(myHigherOrderFunction(isPerfectNumber, intList))
}
}
So I have the following code but I keep getting these errors.
I tried running the function without parameters to see if that would work but it requires parameters.
object Result {
/*
* Complete the 'mapHigherOrder' function below.
*
* The function accepts INTEGER_ARRAY args as parameter.
*/
def mapHigherOrder(args: Array[Int]) {
val intRes = args.toList
val intList: List[Int] = intRes.map(_.toInt).toList
//val intList: List[Int] = args.map(_.toInt).toList // Put your code here
def isPerfectNumber(input: Int) :Boolean = {
// Put your code here
val check_sum = ( (2 until input).collect { case x if input % x == 0 => x } ).sum
if (input <= 1) {
false
}
else if ( check_sum == input - 1 ) {
true
}
else{
false
}
}
}
def myHigherOrderFunction(argFn: Int => String, argVal:List[Int]): List[String] =
{ argVal.map(argFn) }
println(myHigherOrderFunction(isPerfectNumber, intList))
}
Solution.scala:52: error: not found: value isPerfectNumber
println(myHigherOrderFunction(isPerfectNumber, intList))
^
Solution.scala:52: error: not found: value intList
println(myHigherOrderFunction(isPerfectNumber, intList))
You’ve nested isPerfectNumber
and intList
inside mapHigherOrder
, so that means they aren’t visible from outside, hence the errors.