online gambling singapore online gambling singapore online slot malaysia online slot malaysia mega888 malaysia slot gacor live casino malaysia online betting malaysia mega888 mega888 mega888 mega888 mega888 mega888 mega888 mega888 mega888 Do you use Kotlin’s most powerful tool?

摘要: There are many Java-haters and many Java-lovers, but I have hardly seen anyone who doesn’t like Kotlin.

 


images/20220701_6_1.jpg

▲圖片來源:Nejc Soklič on Unsplash

There are many Java-haters and many Java-lovers, but I have hardly seen anyone who doesn’t like Kotlin. From data classs to operator functions, one of the most underrated features of Kotlin is having Higher-Order Lambda functions.

images/20220701_6_2.png

▲Lambda Function in Kotlin

At first, this might not seem like a useful thing as we could define a function instead of having a function for a variable. But here are some amazing use cases for having Lambda functions in Kotlin.

# 5— Making a Function Generator

We know that we return variables and objects from traditional functions that we define; however, with the introduction of Higher-Order Functions, we can return a function type from a function. Let’s have a look:

images/20220701_6_3.png

▲Function generator

If we have a function with customisable content, we can use closures to return a function with a specific intention. We can also add a receiver parameter for the lambda function like this:

images/20220701_6_4.png

▲Function generator with receiver type

# 4— Reduce Code Duplication

As we’ve seen in the previous example, we can remove duplicated and also reduce boilerplate code with the help of Lambda functions.

For example, you are developing an android application that changes the colour and background of a Text on the screen.

images/20220701_6_5.png

▲DON’T (without lambda functions)

images/20220701_6_6.png

▲DO (with lambda functions)

# 3— Ease of Iteration

To iterate over an array, we can use a for loop or the forEach function. However, sometimes, they are not readily available. We can take advantage of lambda functions to make our code readable.

images/20220701_6_7.png

▲The usual ‘Java’ way to do it

images/20220701_6_8.png

▲The modern ‘Kotlin’ way

# 2 — Perform some code before and after the function

If you are from a competitive coding background, you must know that many problems ask you to do a certain amount of test cases in the same execution. You can use this function with lambda parameters to focus on solving the problem rather than setting up your boilerplate.

images/20220701_6_9.png

▲Performing code analysis for the given solution

# 1 — Kotlin DSL

The winner of them all! Kotlin DSL is one of the best use cases of having lambda functions in Kotlin. This allows Kotlin to also create websites and run React code. For example,

images/20220701_6_10.png

▲Source: GitHub

Conclusion

Kotlin itself is a complicated language, in my opinion. I tutor programming online and I have come to realise that teaching Java is way easier than teaching Kotlin. Kotlin was created with the ambition to reduce Java’s boilerplate code and add many new features that Java isn’t built to do. However, once you master the language, it is beautiful and well adjusted to the programmers’ needs.

轉貼自Source: medium.com

若喜歡本文,請關注我們的臉書 Please Like our Facebook Page:    Big Data In Finance

 


留下你的回應

以訪客張貼回應

0
  • 找不到回應