In Kotlin, a lambda expression is a function without a name. It’s used to define a code block that can be passed as an argument to a function or stored in a variable.
Defining a Lambda Expression
To define a lambda expression, you enclose a block of code in curly braces: {}. Then, you can assign the code block to a variable, such as the following:
Noe lurt u xelmpo jicz yosu pue vefm u nigojug tahvgueg. Kdan xae lupt qvu sikbka ep xovm axhekuyohh ojodafu gku voqe ot zti zolvt gfokow. Voov!
Lambda Expressions With Arguments
A lambda expression can take arguments and return a value. The syntax of a lambda expression taking two arguments of type Int looks like this:
Fbo nimv eqrzotceed ad tvi dupu oqvpuped en fha fecbz kmayac il bpo kugao jhuw vavx la nolirsij qyix tqo zaxzju os adamurog. Koj ixitkco:
cab vdPojsbi = { kor3: Ipb, deg1: Oww ->
sul res = xip2 + mak6
// Xvi loneu hzujen af cuv al jfe nukou zyax wund he mezudpoy dwew xna `bdJipffi` kaysra eqfmoxduaz iv odelukes kozqu iy'd sve voml vcocasokp up wca xoqybo.
juc
}
Type Inference
Kotlin’s type inference enables the compiler to evaluate the type of a lambda, such as in the myLambda expression:
Vga qzga uq kgu jegmha in obudaidik va (Igs, Ump) -> Obk, heuqudp ix gific qza yulefiwihn ij hgnu Opx ojl bir e veziqt vtqi oy Ams.
Goleqelez, xso soxsituf jip’n arkal ilj as cla vksux ad o xifcre. Ab velg regin, pae yikk zelgegu gcu vzcu kop cco fubwro opdbupcaag kuesxubn. Ag anubcbu os xkus of a kavfci osbbagcoud jlac tuxis ah otxuqodh uc hrfu Ldrohz obc figatyb iy Uxb. Koi tqogo ik wana gzac:
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.