The functional programming paradigm is very powerful feature captured in many programming languages and Javascript lies no where behind in it. We will see how functional programming finds its place in Javascript.
The idea of developing a program is to create the idea behind a functionality without increasing the complexity of the code. In programming languages there are built in functionalities which help a programmer to implement a code with less hassle. A simple example would be using the looping concept using 'while' or 'for' loop which saves us from performing same task n number of times.
We can define functions over a given data structure like display its contents, add an element etc. Each time we use the array we may need to perform different actions on it. We can in general define a function which takes arrays ans another function as arguments and define it in so that any defined function accepted as arguments can be applied to the array. For eg:-
Here the function keyword is given to indicate it as a function, the arguments array and action are the passed arguments to this function as array and any defined function respectively.
The above defined function now accepts the array and applies the function passed to each element in the array. Such a function is called a higher order function. The function which such a function accepts as arguments is called a first order function.
These higher order functions simplify our code by avoiding useless looping and conditions and can be applied to other functions which reduce its readability but lessen the work within a function. Taking the previous example itself with another function.Eg:-
Like in Lisp and Python here too in Javascript we have reduce or fold which is a higher order function which accepts any function as argument and applies a function say sum, product, or square of sum to elements in a list and returns its result.Eg:-
Likewise we also have an implementation of map method like in Python and Lisp. Map function accepts a list of elements and applies a function to each element in the list and returns the new list.Eg:-
There are many built in functions like split, slice, charAt, which split the text based on tabs and spaces, "," etc, slice to extract a text or patterns from a given text , and charAt to return the text at any particular position respectively.
There are many more functions available with Javascript only few basic ones have been discussed.
The idea of developing a program is to create the idea behind a functionality without increasing the complexity of the code. In programming languages there are built in functionalities which help a programmer to implement a code with less hassle. A simple example would be using the looping concept using 'while' or 'for' loop which saves us from performing same task n number of times.
We can define functions over a given data structure like display its contents, add an element etc. Each time we use the array we may need to perform different actions on it. We can in general define a function which takes arrays ans another function as arguments and define it in so that any defined function accepted as arguments can be applied to the array. For eg:-
function forEach(array, action) { for (var i = 0; i < array.length; i++) action(array[i]); }
Here the function keyword is given to indicate it as a function, the arguments array and action are the passed arguments to this function as array and any defined function respectively.
The above defined function now accepts the array and applies the function passed to each element in the array. Such a function is called a higher order function. The function which such a function accepts as arguments is called a first order function.
These higher order functions simplify our code by avoiding useless looping and conditions and can be applied to other functions which reduce its readability but lessen the work within a function. Taking the previous example itself with another function.Eg:-
function sum(numbers) { var total = 0; forEach(numbers, function (number) { total += number; }); return total; }
Like in Lisp and Python here too in Javascript we have reduce or fold which is a higher order function which accepts any function as argument and applies a function say sum, product, or square of sum to elements in a list and returns its result.Eg:-
function reduce(combine, base, array) { forEach(array, function (element) { base = combine(base, element); }); return base; }
function count_number(array) { function counter(total, element) { return total + (element === 0 ? 1 : 0); } return reduce(counter, 0, array); }
Likewise we also have an implementation of map method like in Python and Lisp. Map function accepts a list of elements and applies a function to each element in the list and returns the new list.Eg:-
function map(func, array) { var result = []; forEach(array, function (element) { result.push(func(element)); }); return result; }
function AreaOfCircle(radius) {
var area = Math.PI * (radius * radius);
return area.toFixed(0);
}
var radii = [10, 20, 30];
var areas = radii.map(AreaOfCircle);
There are many built in functions like split, slice, charAt, which split the text based on tabs and spaces, "," etc, slice to extract a text or patterns from a given text , and charAt to return the text at any particular position respectively.
paragraph.split("\n\n"));
var character = paragraph.charAt(0);
var word = paragraph.slice(0, 4);
There are many more functions available with Javascript only few basic ones have been discussed.