Our task is to find in Kotline Sum of positive number in defined function. We written below code but getting compilation error. Hence need expertise advice for it -

import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*
fun sum(nums:Array<Int>,add: (acc:Int,ele:Int) -> Int):Int {
var res=0
for(i in nums){
res = add(res,i)
}
return res
}
--Editable Code---
fun getLambda(): (acc:Int,ele:Int) -> Int {
var lambda = { acc: Int, ele: Int -> acc + ele }
return lambda
}
---END--
fun main(args: Array<String>) {
val numsCount = readLine()!!.trim().toInt()
val nums = Array<Int>(numsCount, { 0 })
for (i in 0 until numsCount) {
val numsItem = readLine()!!.trim().toInt()
nums[i] = numsItem
}
val lambda = getLambda()
val result = sum(nums,lambda)
println(result)
}

fun sum(nums: Array<Int>, add: (acc: Int, ele: Int) -> Int): Int {
var res = 0
for (i in nums) {
res = add(res, i)
}
return res
}
//sampleStart
fun getLambda(): (acc: Int, ele: Int) -> Int {
var lambda = { acc: Int, ele: Int -> acc + ele }
return lambda
}
//sampleEnd
fun main() {
val nums = arrayOf(1,2,3,4,5) // Simplified to run in the playground
val lambda = getLambda()
val result = sum(nums, lambda)
println(result)
}

As I said I won’t do your homework. What did you change based on my comment?

How do you know that. Do you have an example with the correct result? What is the error you get?
It’s really hard to help you unless you give us some more information.

Your solution:
fun sum(nums:Array,add: (acc:Int,ele:Int) → Int):Int {
var res=0
var newNumsArr= nums.filter{ it > 0 }
for(i in newNumsArr){
res = add(res,i)
}
return res
}
fun getLambda(): (acc:Int,ele:Int) → Int {
var lambda = {x:Int,y:Int → x +y}
return lambda
}

fun sum(nums:Array,add: (acc:Int,ele:Int) → Int):Int {
var res=0
var num= nums.filter{ it > 0 }
for(i in num){
res = add(res,i)
}
return res
}

fun getLambda(): (acc:Int, ele:Int) → Int {
var lambda= { acc:Int, ele:Int → acc + ele }
return lambda
}

fun main() {
val numsCount = readLine()!!.trim().toInt()

val nums = Array<Int>(numsCount, { 0 })
for (i in 0 until numsCount) {
val numsItem = readLine()!!.trim().toInt()
nums[i] = numsItem
}
val lambda = getLambda()
val result = sum(nums,lambda)
println(result)

}
here bro this is 100 percent working, all 4 test cases successfully passed.