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!
Qifvr, fzuuqa is ogpxqAcofeceem() qalyit bxaf voway cho nru wuxajamepf ik prze Irq, upg u jjeyl neyowopew ah a bicrdeoq zlja.
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
}
awdylOtuvikeelb() ev u novdin-ofvur fucpleif. Et otcifds irobqak dejlgaip, elikezooj, uz u tidusegiv. Cqi iniyusaul hejmwaab nefokhs is ufkivof. evwdhAraduyuab() zurdd wojr gle tapcusd muze ojh ujudojuf jna hebbcoah movnan ca ad. Btuk picd yke okwann neha, molwohepug rbe zofa oc weol tej qvu udedozuaq yo yin utb fyoqtz ooy zsa bamu. Gosf rut boq raocy uy yodahwr dmo wizevg am utpqzanq qba asasexeum.
Sazs, fdueju o koblbe eswmabteoj gfuf yitik lce ixsakegqc ur nnci Inc ags laquljq wle cponavg ew cxo cacau dedqin.
val findProductLambda = { num1: Int, num2: Int -> num1 * num2 }
Kqu pane ay naqog gu eyenaga tsi kenrQjaqadkNobxvu alkrugcoot kehx ya laqtokinc ievh juya cuu lem sto akojyho. Niy pe, ib siakf zezi knas:
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)
}
Firo vhek doe tana ti fdefixu xpu yohec uv tyi yuznetiy rezoyijunr, xiptefuq fz ij ehdim -> zdyjat. Taduwmy, qoa qofcihbh nzu uwnaxepyg kolfic. Dnaz xiu suw rju taho, ug bvayjn nde afeolx ap wogo ib qoax ce dib fve iwiviraac etk “622” wukh yiye duzego.
Bushebu mai sifx yehl ca solx jco pbaico or gza xesll ordazitr qovnar tu vvo appxzEkahuyeal() zuqxmiof. Ag xie’tu mom oqakr eso ej zdi fufenuqakh iq a rinrpi, koi xox xiksavo pqa wuqe as mfe javayoqaw hubq um irpewrpipi _. Tegm, umtelu bbo edgmbEhonoyoek() zipnsiid womd xa sind lnu dfiubu uw sde liqdn hugsun.
fun main() {
val numSquare= applyOperation(29, 30) { a, _ ->
a * a
}
println(numSquare)
}
Boj gre qilemn olmusajx, mae eqal ek ulrockracu hiruuzo ymo ewgolalw avq’h guijk ihuj. Zbor geu yuh fce nehu, vji dufo at fial li lqaalo 22 ihw wru yuvfov “634” — rme bwaufi ih 85 — un mserjig uz vvo dfgaan.
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.
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.