Sobes.tech
Junior — Middle

Can you explain the concept of the functional programming paradigm?

sobes.tech AI

Answer from AI

The functional programming paradigm is based on using functions as the primary building blocks of a program. In it, functions are considered as pure mathematical functions — they have no side effects and always return the same result for the same inputs.

Main features:

  • Immutability of data: data is not changed after creation; instead, new copies are created with modifications.
  • Absence of side effects: functions do not alter the state outside themselves.
  • Higher-order functions: functions can accept other functions as arguments and return them.
  • Recursion instead of loops: recursion is often used for repetition.

Example in Java using lambda expressions and Stream API:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> doubled = numbers.stream()
    .map(n -> n * 2) // higher-order function map applies a function to each element
    .collect(Collectors.toList());

System.out.println(doubled); // [2, 4, 6, 8, 10]

Here, we do not modify the original list but create a new one by applying a function to each element.