A programming paradigm known as functional programming avoids state and changeable data and regards computing as the evaluation of mathematical functions. In other words, immutable variables and side-effect-free code are encouraged by functional programming (FP). It uses a declarative programming approach. In contrast to an imperative style, where the primary focus is “how to solve,” it places a greater emphasis on “what to solve.” You can attend the Python online classes to learn more about Functional Programming (FP) in Python.
Concepts of Functional Programming
- Pure Functions
Two characteristics of pure functions are:
- They have no negative consequences.
- If given the same input, they always create the same output. This indicates that no changeable state can be a dependency of the function.
The following causes of side effects include:-
- altering data that is present outside the function’s boundaries.
- Changing the value of arguments in the function.
- causing an error or throwing an exception.
- receiving user input or printing to the terminal.
- examining or adding to a file.
- Immutability
A variable’s value cannot be changed in functional programming once it has been initialised. This means that instead of altering the old list when we need to modify values in it, we must construct a new list with the updated values. The immutability of variables in FP is advantageous since it maintains the state as a program is run. Python supports immutable data types such as int, float, complex, text, tuple, frozen set, and bytes.
- Recursion
In FP, if-else clauses and loops are avoided because they produce distinct results with each iteration. All iteration tasks in functional programs employ recursion instead of loops. Recursion is a function that repeatedly invokes itself up until an exit condition is satisfied.
- First-Class and higher-order functions are available
Functions can be utilised just like any other value in FP because they are regarded as a data type. Variables can be given functions, kept in data structures, passed as arguments, or used in control structures. For instance, we could use functions to fill an array, provide them as parameters, or save them in variables.
The term “higher-order function” refers to a function that uses another function as a parameter or returns a function as an output.
Some of Python’s built-in higher-order functions are Map, Filter, and Reduce.
Map as a higher-order function.
There are two arguments for the map function. Iterable and function are the first and second arguments, respectively. The passed function is subsequently applied to each item in the iterable.
Filter as a Higher Order Function
Based on the criteria you specify, the filter function makes it simple to isolate matched records from a broader set of data. There must be two arguments. Iterable and function are the first and second arguments, respectively. For each iterable element for which the filter function returns true, a sequence is returned.
Reduce as a Higher Order Function
Reduce takes a function and a sequence as inputs and outputs a single value that is calculated as follows:
- The function is initially called with the first two elements in the sequence, and the output is then returned.
- Using the output from step 1 and the subsequent value in the sequence, the function is then called once more. Until all of the items in the sequence have been iterated, this process is repeated.
Functional programming Advantages
- Pure functions always yield the same results; they are unaffected by outside variables. Programs are simpler to test and debug as a result.
- Because they execute separately without changing state, parallel or concurrent programs can be written effectively.
- It supports the idea of lazy evaluation, where values are only evaluated and stored when necessary.
- With Pure Functions, FP enables stronger encapsulation than OOP.
- Since FP programs are composed entirely of pure functions, we can readily reuse them.
Functional programming Disadvantages
- Recursion and immutable values could have a negative impact on performance.
- Sometimes writing pure functions can make the code harder to read.
- The state in functional programming is absent. Instead of altering existing things, they always create new ones in order to carry out their operations. FP Applications use a lot of memory as a result.
- Writing pure functions is simple; the challenge comes in putting them together into a comprehensive application.
Conclusion
This article gives you a brief overview of functional programming and should help you understand its advantages. Python enables us to code declaratively and functionally. Even more, it supports a variety of widely used functional constructs like lambda, map(), filter(), and reduce() that may be used to create parallelizable, compact code. Check out the Python online training to learn more.