Functional programming using JavaScript - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Friday, August 28, 2020

Functional programming using JavaScript

Introduction: - Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles.

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.

Functional code tends to be more Couse, more predictable and easier to test than imperative or object-oriented code, but if you are unfamiliar with it and the common patterns associated with it, functional code can also seem a lot denser.

Different states in functional programming

·       Pure functions

·       Functions composition

·       Avoid shared state

·       Avoid mutating state

·       Avoid side effects

Pure functions is a function which given the same inputs, always return the same output and has no side effects.

Shared State is any variable object or memory space that exists in a shared scope, or as the property of an object being passed between scopes.

Immutability is an object that can’t be modified after its created. Conversely, a mutable object is any object which can be modified after its created.

Declarative programs abstract the flow control process and instead spend lines of code describing the data flow.

Imperative programs spend lines of code describing the specific steps used to achieve the desired result, the flow control

Functional Programming Favors

·       Pure functions instead of shared state and side effects.

·       Immutability over mutable data.

·       Functional composition over imperative flow

·       Declarative rather than imperative code.

·       Expressions overstatements

·       Containers and high order functions over ad-hoc polymorphism.

 

Features of Functional Programming

First-class of citizen functions- good about functional programming is its functions are first-class citizens function

[code]

executeFunctions(x,y)

{

Const add=(x,y)=> x+y;

Const subtract=(x,y)=>x-y;

Console.log(‘sum: ${add(x,y)});

Console.log(‘difference: ${subtract(x,y)});

}

[/code]

 High-order functions are  function that gets a function as an argument

E.g.

[code]

Function greaterthan(n)

{

Return x=> x >n;

}

Let greaterThanFour =greaterThan(4);

Console.log(greaterThanFour(8));

 [/code]

Function Composition

Function composition is an act of composing/creating functions that allows you to creating functions that allow you to further simplify and compress.It also return another function as its output other than numerical/string values.

E.g.

Var compose = (a,b) =>(x) => a(b(x));

In this there is a variation in the way we compose functions by the name of Monads, we will discuss about Monads in different post in details.


Benefits of functional programming

Functional programming became popular because of the following reasons

·       Pure function is easier to reason about.

·       Debugging is easier.

·       Testing are easier and pure functions lend themselves well to technique

·       Programs are written at a higher level and are therefore easier to comprehend

·       Functions signature is more meaningful.


Where is functional programming used?

Functional programming is less popular than imperative programming. Nowadays functional programming is used in industry, education including scheme, clojure,Racket, Haskell.

To summarize we can say that the major goal of functional programming is to minimize side effects which can be accomplished by isolating them from the rest of the code. Separating the effects from the rest of the logic which makes a program easier to maintain, test, and debug.

Functional programming is becoming famous in the industry. This emerging trend of this is driven by the adoption of Scala as the main programming language for so many applications.

Scala fuses functional & object-oriented programming in a package.it works with both java and javascript.

Let's take an example to understand functional programming in scala.

We will begin learning to write programs in scala language by combining pure functions.New to scala and to functional programming. how one can learn a new language is code, code, and code.

So, let's take a simple and complete example to understand scale.

[code]

A comment!
/** A documentation comment */
object MyModule {     
  def abs(n: Int): Int ={
    if (n < 0) -n     
    else n
}
  private def formatAbs(x: Int) = {  
    val msg = "Absolute value of %d is %d"      
    msg.format(x, abs(x))    
  }
 
  def main(args: Array[String]): Unit =     
    println(formatAbs(-30))
}

 [/code]

We declare an object name MyModule scala code has to be in an object or a class and we use an object because its easy we put our code inside it, between curly braces.

MyModule object has three methods def, abs,formatabs, and main.we will use the term method to refer to some function.

By now you know the basics about functional programming and to start working on projects explore and learn from our website with detail and easy presentation from an experienced developer in this field

http://skillbakery.com/courses/functional-programming-using-javascript.html

 

No comments:

Post a Comment

Post Top Ad