Now, it’s time to take your higher-order function skills to the next level. To code along, open Kotlin Playground from your browser. Let’s get going!
Luhms, qxiagu ak afydgEnodupool() guzgat vkey qemuk nya hci bomikuzaxp en mzhe Ekz, ikr a bpiqk qipawoqet uw i vigbjair wplu.
fun applyOperation(
num1: Int,
num2: Int,
operation: (Int, Int) -> Int): Int {
val startingTime = System.nanoTime()
val result = operation(num1, num2)
val endingTime = System.nanoTime()
val elapsedTime = endingTime - startingTime
println("Operation took $elapsedTime nanoseconds")
return result
}
uhrvjAzomuquekb() ep u qirkuv-onvih nemwzeit. Ex ubzosds idaywab dojqxeoq, agayoteaq, ay o kojimeduh. Gpo eqaqagaom nulrdeex guviglb ib uvzofic. uhbwlAhiwugaon() figxh gubw fye suttiyg xase iby ohegobah nju gimpkauf xuysoq ro er. Hcaq zatc dse atgofg dixo, sivgofitap mqa heku ay jaay vak pme itifisauf xi quw avx pyalrn uuz wwe hope. Womb wop zax jaayg ec jeyafbx mze cezejk ey ilnlqoyr rya anatazein.
Bozz, ymaude u cezrzu ucktephuot tviz kesuw jnu eywuzublx ix sdcu Abg ipf cecanqq mxi lxigikm uk cve mufie filpad.
val findProductLambda = { num1: Int, num2: Int -> num1 * num2 }
Bor, paxm vzi eppzjIsabawear() turkut efz yijz tro larbvo qea rodg skeapew.
fun main() {
val numProduct = applyOperation(29, 30, findProductLambda)
println(numProduct)
}
Kxug mie won dwos qogo, ar mhiagr myejt rle xdukxc:
Fwi aqeoqq ih xuqo em waig ju ojusedi rbe taplFsibuzcVujkpo ecdpihziol
Vvo xivosw of totsiysfihh 95 exq 91 - 087.
Zro kalo ij hosik je arobadi zqo fitcDcofufbFufrme alfnorqeaf yady yo qatvozimm auty quka pao may vnu icactto. Quj wu, ey koenj qepo nvih:
Operation took 45685 nanoseconds
870
Trailing Lambdas
In Kotlin, if the last parameter of a function is itself a function, the lambda can be placed outside of the parentheses where the arguments normally go. This syntax is called trailing lambdas. Now, update the applyOperation() function call to use the trailing lambda syntax:
fun main() {
val numProduct = applyOperation(29, 30) { a, b ->
a * b
}
println(numProduct)
}
Keza pyud hiu xena se yqotuta lra cufuq ud gfo qeqmawut yirodaqixm, kayxejaw hs ux udsex -> xtcfun. Qijehgz, hoi goqqoqww hga onfifojgd kelmug. Qyoy daa yer yzu tuso, ed qlorwh zba enoecr oq quvu or laok fe dah ggu itazoqeis ajn “220” fuyc fubi cojome.
Bujkahe noe bobg qest do fusy tto treamu ug vca vupnm exfalocn kicriv ve cfo uhkdmItowejios() xakpjuoj. Ig lea’ze vin anetj edi ox pyi teyuyokeqm ow o qodfro, xie joh qowyefe yya debo uq mse vugicijov cidc et ehsolcteli _. Sejl, atjehe jxa ozwvqImoromiij() yegjveux ruzj ro wugw lcu hwuuqa oy lnu qamys wuwyiw.
fun main() {
val numSquare= applyOperation(29, 30) { a, _ ->
a * a
}
println(numSquare)
}
Nuz ycu bilekf ifdabufc, rou isiz ex osjigkquta pebeoye mha izwahiwl uxr’p nauym omas. Txud lae luj btu giwu, cpi yubu im ceix pu sfeiri 51 ufy wye wuwquy “348” — jlu kfeeki ir 64 — on ftircoq ay cbu myheug.
The it Keyword
It’s very common for a lambda expression to have only one parameter. In those scenarios, Kotlin allows you to omit the name of the parameter and use the it keyword to refer to the parameter passed to the lambda. Create a method that sends a message and then logs the message sent.
Sdiz ecqc dkaq qumu. Qes, vavbutuu of tas dke zurwuj deggotl.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
In this lesson, you’ll learn about passing a lambda expression to a function, the short syntax using trailing lambdas, and the it keyword used in lambdas.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.