Tuesday, August 7, 2018

Understanding Functional programming basics in javascript with examples

In this post, We will discuss different programming paradigm related to functional programming, how it will be implemented in javascript.

Functional Programming

It is one of programming type to handle computations and mathematical applications. Haskell, Clojure, Python, Erlang, and Scala are complete functional programming languages. Javascript is not strictly functional programming languages but it supports some of the techniques in it.

Applicative Programming 

It is one more pattern for a function as parameter other function and executes for each element in an array or collection. To make it happen, javascript introduced First class objects/citizens.
Javascript already implemented this paradigm with Array and String methods like forEach method, filter, every(), Map() methods. These methods are iterated each element and apply function at each element.
We will see examples in the next sections
Functions are also objects in javascript
Functions also contain properties

let us see simple function creation
console.log(testmethod); // returns function code
console.log(testmethod.name); // returns testmethod
 function testmethod() {

aAter running the aboe code, It just returns function. Let u
Functions also has properties, testmethod.name prints name of the function

First Class Functions

What are First Class Functions?
First class functions in any programming are called for functions if Functions can be treated the same as variables. whatever you do with variables, you can do everything with javascript functions also.
that means functions are saved to variables, passed as a parameter to function and returned from the function. 
It is also called  First class citizens or First class objects of Javascript
We will see detailed tutorials using functions as equal to using a variable  with examples below

  • Functions can be assigned to variables/constants/objects/arrays
  • Functions can be passed as a parameter to a function 
  • Functions can be returned from a Function

Function saved to variables/constants/objects/arrays 

Here we assigned the anonyms function to the variable. we are calling the function with that variable. Named functions can also be called like this. Functions and variables/values are treated the same in javascript
Function assign to a variable
The function also can be assigned to variables
let functionVariable = function method() {}
Assign to a constant 
The function also can be assigned to constants
let functionVariable = function method() {}
const function1 = function() {
   console.log("function printed");
Function assign to Object 
This is an example of assigning or storing function like a variable in Object
let obj = { method : function(){} }
Function save to array 
This is a syntax of storing function like an element in arrays
myarray.push(function method() {})

Passing as a Function argument to other functions 

This example, passing a function as an argument to function Here function2 is passed as an argument to display function function2 is a callback function
function function2() {
   return "function2 ";
function display(helloMessage, name) {
  console.log(function2() + name);

Function returns function like a variable value

function mainfunction() {
    return function(){
 return "return function as value"}
From the above functions examples, We can say javascript in functions are treated as a variable. we can easily say first class citizens.

High Order Functions are functions which takes a function as an argument and return the function as parameters

Data is immutable in Functional programming 

In the functional programming, state or data is immutable meaning not able to update it. 
For a variable, 
If you want to change data in a variable, you need to create a new variable. To achieve this use const keyword for a declaration 
For arrays 
you need to create a new array and concat existing array You can use Object.assign() method or spread operator to achieve this. 
You can check here for more information.
For objects
Data in the object will never be changed, So you need to use Object.assign for cloning the object and update it.
In functional programming data is never changed, You need to create a copy of data to change it.
functions provide copy of data and javascript native map and reduce methods are supported for returning copy/creation of existing data.


Note: Only a member of this blog may post a comment.