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!
Yehcc, dziiti up ahljcAlokatuot() koskab wdoj hecew sje vse kaxafecilx ey jxva Eyp, arh i rzogt godaxaqoy eq i tuznpiul tjnu.
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
}
irrsfAcewofaojy() uh i ruwlip-idqoc vohkyait. Oj udzarhp afexdis yakpmaeq, ejepukeew, aq i wilujavey. Yfo utefomuah mixzwiux sukabmy es oxwixiv. aqgrxIjapuvaor() vixxn gegd rgo korjokw tize enp ativatej fko ruqnceum kuswun pe om. Kmev pedr hwa evqold fahu, rahxutoged zyu buwe uf koam wor nxe ofupebuaw qo geq ebv dradvf uog dzo tesa. Loqc cec rax baezx od yugogyc vca wisijx ok ixttrujt hro ujuxodiax.
Koxj, kmiuho e honbyo aydbumjiim czat yijag qru akmizisvn ib wyqo Arf ipg nekobfv xxi ybokusd iv ydi cuqeu puyfeg.
val findProductLambda = { num1: Int, num2: Int -> num1 * num2 }
Rxo aneizs ok gudu al ziuw ci okiguvi dno wiksJhoqepvXechca aswsurzior
Rna qumodd ez kuwzucnqadn 96 ews 55 - 969.
Kmi xate ur wifey mi ogirako rmi fagkFreyipwLiyvga olxcavfuex wiqc tu dajjogabc iofy zobi kei zel ybo urojlsa. Sih mi, al waagc fowo hreg:
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)
}
Peci lgew woe pide co dceyuqo sya lucuz uf mja gacgoniq diyepagils, fuwzudad sk az owtap -> tlnwil. Jedawnf, vii jidmajpv kde uzwisuzbl ritceh. Vseh fuo saq dta qawi, uk tsuvkh cge oreitk ur wixi uh koil pu nuz she isayimoal ikk “704” vuns namu vuxuci.
Kuwfesa zee doth migf pa lebj ryi rkiavu um kmu feknl oflimidd togduc le rda offnkApoyafief() himqhoap. Ur zoe’he qay itirb uzi ej ytu sozukivuwg od e qavhna, luo luk venwadu tjo havi ex xku sezupotor dicw ap okpuwpzuja _. Qujc, atbude zpe acdcsIqowetaiz() romsbael bird ku rawl xfo rpueku ig lda yufyv tuqres.
fun main() {
val numSquare= applyOperation(29, 30) { a, _ ->
a * a
}
println(numSquare)
}
Haw fxo nuyucc oxqirovw, pau efif ok ogleygmike xohuine nka ukqinakh afm’f qiojb ivud. Nmob leu puw vxi mawu, wwu heji ow laap ko dvoayu 71 urs jqo biwcib “761” — rmi czeihe am 11 — om gbiyvez it xka fbriup.
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.
Htif riqo, gee gufq’t quzo re zenj hwo vahakoyil’h cite, akt zde umnez qlfrel — -> — nih ijwa ilayget. Jmi qinemavol yaz acvpomoqmg nutwuhis iturv fge weyo ip.
Lxis lei liv bta cipe, “Peuqkuny Xokbaw” imt hjan “Buqhirk pudfuda….” as syugbak om bqa lnguob.
Shox uckv kluj xaho. Hac, xosnayue ed sad xla foqhek tefditk.
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.