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
}
High-Order Functions:- It means that functions can return functions or receive other functions as parameters. i.e.
var add = function(a){
  return function(b){
    return a + b
  }
}

var add2 = add(2)
add2(3) // => 5
Pure Functions:- Pure Functions mean that the function doesn't change any value, it just receives data and output data, just like our beloved functions from Mathematics. That also means that if you'd pass 2 for a function f and it returns 10, it'll always return 10. Doesn't it matter the environment, threads, or any evaluation order. They don't cause any side-effects in other parts of the program and it's a really powerful concept.

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
Immutable State:- Immutable State means that you can't change any state at all (even though you can get a new state).

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 ProgrammingOOP
Uses Immutable data.Uses Mutable data.
Follows Declarative Programming Model.Follows Imperative Programming Model.
Supports Parallel ProgrammingNot suitable for Parallel Programming
Its functions have no-side effectsIts methods can produce serious side effects.
Flow Control is done using function calls & function calls with recursionFlow 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
As we can see, our function has only one variable called total that is updated on every iteration. This is clearly a case of a mutable variable.

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
This time we are not updating any variables and are using recursion, which is the functional programming way of doing loops.

Want to learn Python & Django

Comments

Popular posts from this blog

PPC - SomeOne SpenDing - SomeOne EarNing

Web scrapping tools,Sooo Muuch Data - Analysis Needed !

Must kNow Web Dev toolS - 2OI7