Now is the Time to Learn Functional Programming !
What is Functional Programming?
Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and collocated with methods in objects. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time.Functional code tends to be more concise, more predictable, and easier to test than imperative or object oriented code but if you’re unfamiliar with it and the common patterns associated with it, functional code can also seem a lot more dense, and the related literature can be impenetrable to newcomers. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, Java etc.
Functional programming languages are categorized into two groups, i.e.
Pure Functional Languages:- These types of functional languages support only the functional paradigms. For example − Haskell.
Impure Functional Languages:- These types of functional languages support the functional paradigms and imperative style programming. For example − LISP.
Functional Programming Characteristics:
- Function Closure Support
- Higher-order functions
- Use of recursion as a mechanism for flow control
- No side-effects
- A focus on what is to be computed rather then how to compute it
- Referential transparency
Functional Programming Features:
First-Class Functions:- It means that you can store functions into a variable. i.e.var add = function(a, b){
return a + b
}
var add = function(a){
return function(b){
return a + b
}
}
var add2 = add(2)
add2(3) // => 5
Closures:- Closures mean that you can save some data inside a function that's only accessible to a specific returning function, i.e the returning function keeps its execution environment.
var add = function(a){
return function(b){
return a + b
}
}
var add2 = add(2)
add2(3) // => 5
Advantage of Functional Programming
- Easier to write parallel code. The reason is immutable data structures.
- More powerful expressions making the code more terse. Monoids, functors, lambdas to name a few.
- Extensive type checking and a very powerful type system (in typed ones).
- Homoiconicity in languages like LISP, which makes writing DSLs extremely easy.
Functional Programming v/s Object Oriented Programming
Functional Programming | OOP |
---|---|
Uses Immutable data. | Uses Mutable data. |
Follows Declarative Programming Model. | Follows Imperative Programming Model. |
Supports Parallel Programming | Not suitable for Parallel Programming |
Its functions have no-side effects | Its methods can produce serious side effects. |
Flow Control is done using function calls & function calls with recursion | Flow control is done using loops and conditional statements. |
Execution order of statements is not so important. | Execution order of statements is very important. |
Functional Programming in Python
Python is not a functional programming language, but it is a multi-paradigm language that makes functional programming easy to perform, and easy to mix with other programming styles. Lets see the example of calculating total sum of values in a list. In this example we are using an imperative style function.Calculating total sum of values using normal method
def sum_lst(lst): total = 0 for number in lst: total += number return total
Now lets try a functional approach:
def sum_lst(lst): if not lst: return 0 else: return lst[0] + sum_lst(lst[1:]) # values are returned but no variable is changed
Comments
Post a Comment