A gente passa a vida inteira escrevendo historias, não sei todo mundo no sentido literal, como eu, mas certamente em algum outro sentido. É preciso uma imaginação praticamente nula pra não pensar no…
Functional programming is very useful it makes many development tasks easier to implement and makes our code more readable and concise and less prone to errors and bugs.
This is only a big reason to use functional programming while writing Kotlin code because it provides abstraction and helps us to write concise, comprehensive and much more readable and understandable code.
Kotlin gives support to functions as a first-class citizen basically means, functions in the Kotlin can pass around as parameters in another function or can assign functions to a variable.
Kotlin allows us to write functions in very less concise manner example
fun addValue(x: Int, y: Int) = x +y
No return written statement no return type Kotlin compiler automatically infers written types in most cases. We can also write a function which has a return statement and return type also.
Higher-order function and lambda expression
The higher-order function simply a function that accepts or return another function.
Using the lambda and a higher-order function together may save time and helps to write very short and readable code.
Some build-in frequently used higher-order functions of Kotlin filter which filters the elements based on the given predicate, a map which maps each element to some other elements according to condition, take which takes first n elements of the list and drop which drops first n elements of the list. Below is an example of these basic functions.
(1..100).filter(element -> element %2 ==0)
Returns all the even numbers from 1 to 100 like 2, 4, 6….100.
(1..100).map{element -> element*2}
Returns 2, 4, 6, 8…..200.
(1..100).take(5)
Return 5 elements 1,2,3,4,5
(1..100).drop(90)
Drop the first 90 elements and return result 91, 92…100
Chaining function
In the Kotlin we can apply more than one function in the chains like below. This is useful because in many situations result that we want doesn’t give by one function so we want to apply another function on the result of the previous function and chain continues to get the expected answer.
Lazy sequence
The idea of lazy evaluation is to evaluate it when it needed. It increases the code performance since it is only executed when it's required and only which part is needed. It avoids unnecessary computation on large data.
Some other useful higher-order functions
Scoping function let
Scoping function let returns the result of lambda expression and takes the object it is invoked upon. Variables declared inside the scoping let function can not be accessed outside of the expression.
Higher-order function with
Function with is used to change instance properties without the need to call dot operator over the reference every time.
Inline functions
Using the higher-order function and lambda expression increases the cost because all stored as objects and for these objects compiler implicitly creates class and allocate memory for both classes and objects and this increases memory and time overhead. To reduce the memory overhead for higher-order function and lambda expression we can use an inline function which simply tells to the compiler do not allocate memory and just copy this to the place of call.
Check for more detail
Determining a person’s gender as male or female, based upon a sample of their voice seems to be an easy task, because human can easily hear the difference between a male or female. However, designing…
As Smart Cities integram os setores público e privado, por meio das mais avançadas tecnologias, com o objetivo de tornar as cidades mais eficientes e seguras Uma cidade em que hospitais, delegacias…
Copernicus Masters is like Hollywood awards but for scientist and companies working with satellite data