How To Write A Lambda Function with Kotlin

this is a basic kotlin function that ask for user name
fun main() {
//1st method
val count = manipulateInput("Enter you name here: ") { message ->
// this way your user input is store in "message"
// you can do what ever you want with whatever data is in "message"
val count_words = message.length
println("The number of characters in '$message' is '$count_words' ")
return@manipulateInput "$message-Kotlin"
}
//outside the lambda
println()
println(
"The final count of the characters is $count")
}

fun getInput(message: String): String {
print(
message)
return readLine()!!
}

fun manipulateInput(
string: String,
function:(str:String)->String
):Int{
val message = getInput(string)
val count = function(message)
println(
"character length has been modified")
return count.length
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store