DEV Community

carter
carter

Posted on

Functional Programming in Kotlin pt. 1

In Kotlin you can write pure functions and harness the power of functional programming in a similar fashion to Scala.

Side-Effects are state changes that persist out side of the local return value of a function. Mutator methods of a class are an example of functions that create side-effects, these kinds of functions are called procedures.

Referential Transparency is the idea in functional programming where replacing a function call with it's returned result will not change how the program behaves.

Pure Functions are referentially transparent because they do not create any side-effects.

The benefit of referential transparency in pure functions show up quiet apparently: The program becomes no less complex to reason about than an algebraic expression.

Here is an example of a pure function:

fun abs(n: Int): Int = Math.abs(n)
Enter fullscreen mode Exit fullscreen mode

Here is an example of an impure function:

class Burger(var eaten: Boolean) {
  fun eat() {
    this.eaten = true // changing the state is an example of a side-effect
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
robert_webb_7b07ab185774b profile image
robert webb

I know a real professional hacker who has worked for me twice in the past month. He is very good at hacking anything concerning databases, phones, social media, and even credit report fixes. He offers legit services. He also helps retrieve accounts that have been taken by hackers. Contact him at cybergoldenhacker at gmail dot com.