Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

lambdas-higher-order

Lambdas and higher-order functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Lambdas and higher-order functions: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Lambdas and higher-order functions in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Lambdas and higher-order functions in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions are first-class values in Kotlin—lambdas pass behavior to APIs like map and filter. Syntax is leaner than anonymous inner classes in Java and closer to JavaScript arrow functions.

Lambda syntax

val double: (Int) -> Int = { it * 2 }
list.filter { it > 0 }

When a lambda is the last argument, trailing lambda syntax moves it outside parentheses. Single-parameter lambdas can use it.

Higher-order functions

Functions accepting or returning functions enable reusable patterns—e.g. fun retry(times: Int, block: () -> Unit).

Important interview questions and answers

  1. Q: What is it?
    A: Implicit name for the single lambda parameter when omitted.
  2. Q: Lambda vs anonymous object?
    A: Lambdas compile to invokedynamic on JVM; objects needed when implementing multiple methods or carrying state.

Self-check

  1. How do you declare a function type (Int) -> String?
  2. When can you use trailing lambda syntax?

Tip: Use trailing lambda syntax: list.filter { it > 0 } when the last arg is a function.

Interview prep

it keyword?

Implicit name for single lambda parameter.

Trailing lambda?

Last function arg can move outside parentheses.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • it parameter?
  • Trailing lambda?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump