> For the complete documentation index, see [llms.txt](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/lambda/functional-programming-concepts.md).

# Functional Programming Concepts

**Pure Functions:**

**Pure functions** are functions that always produce the same output for the same input and have no side effects. They do not modify any external state or variables and have no dependencies on the state of the system. Pure functions are deterministic and predictable.

**Example of a Pure Function:**

```java
import java.util.function.Function;

public class PureFunctionExample {
    public static void main(String[] args) {
        Function<Integer, Integer> addTwo = number -> number + 2;
        int result = addTwo.apply(3); // Output: 5
        System.out.println(result);
    }
}
```

In the above example, the `addTwo` function is a pure function because it always produces the same output (input + 2) for the same input.

**Immutability:**

**Immutability** means that once an object is created, its state cannot be changed. In functional programming, immutable objects are preferred because they simplify reasoning about the code. Immutable objects are thread-safe and reduce the chance of bugs related to mutable state.

**Example of Immutability:**

```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableExample {
    public static void main(String[] args) {
        List<String> mutableList = new ArrayList<>();
        mutableList.add("Java");
        mutableList.add("Python");

        // Creating an immutable copy of the list
        List<String> immutableList = Collections.unmodifiableList(new ArrayList<>(mutableList));

        // This will throw an UnsupportedOperationException
        immutableList.add("JavaScript");
    }
}
```

In the above example, `immutableList` is an immutable copy of `mutableList`. Attempting to modify the immutable list will result in an `UnsupportedOperationException`.

**First-Class and Higher-Order Functions:**

**First-class functions** treat functions as first-class citizens, allowing them to be passed as arguments to other functions, returned as values from other functions, and assigned to variables.

**Higher-order functions** are functions that take one or more functions as arguments or return a function as a result.

**Example of First-Class and Higher-Order Functions:**

```java
import java.util.function.Function;

public class HigherOrderFunctionExample {
    public static void main(String[] args) {
        // Higher-order function taking a function as an argument
        Function<Integer, Integer> square = number -> number * number;

        // First-class functions: passing a function as an argument
        int result = applyFunction(square, 3); // Output: 9
        System.out.println(result);
    }

    // Higher-order function taking a function as an argument
    public static int applyFunction(Function<Integer, Integer> func, int number) {
        return func.apply(number);
    }
}
```

In the above example, `applyFunction` is a higher-order function that takes a function as an argument and applies it to a number. The `square` function is a first-class function passed to `applyFunction`.

Understanding and applying these functional programming concepts can lead to more robust, maintainable, and parallelizable code, especially in the context of concurrent and distributed systems. Functional programming encourages the use of these concepts, making your code more predictable and easier to reason about.
