Lambda

We already discussed how, if a function's last parameter is a lambda, it can't be passed outside the parenthesis and inside curly braces, as if the lambda itself is the body of a control structure.

We covered this unless function in Chapter 2, Getting Started with Functional Programming, in the section, First-class and high-order functions. Let's have a look at the following code:

fun unless(condition: Boolean, block: () -> Unit) {   if (!condition) block()}unless(someBoolean) {   println("You can't access this website")}

Now, what happens if we combine vararg and lambda? Let's check it in the following code snippet:

fun <T, R> transform(vararg ts: T, f: (T) -> R): List<R> = ts.map(f)

Lambdas can be at the end of a function with a vararg ...

Get Functional Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.