Power of Kotlin : Generate Fibonacci series in 6 lines of code with Lambdas and Higher Order Function Rajesh Hadiya Follow Jan 27, 2019 · 2 min read

Yes, you read that right. You can generate Fibonacci series in only 6 lines of code with power of lambdas and higher order function.

Kotlin is JVM language, means it runs on same platform used by Java. Although Java is very popular and great language, many of us may consider it as old language which is not-so-suitable for modern programming requirements. Here comes Kotlin into picture. Kotlin is very powerful and feature rich language having combine power of java and it’s own. It provides some great features including Avoidance of NullPointerException(NPE), Co-routines, Data classes etc.

Kotlin support Lambdas and Higher Order Function(HOF) which was not available into Java (Though added later as Java Streams). Both this feature is very powerful and can make code neat and clean if used properly.

Lambda expression is simply annonymous functions that take input and return output and represented as expression using arrow symbol like x -> x+y. Higher Order Function is a function which can take another function as input and/or return function as output.

Before we start, here are basic requirements to understand this article better.

Basic understanding of Kotlin Little familiarity with Lambdas Piece of brain (if needed)

So let’s get our hand dirty with some code