Functional Programming - Functions

Functional Programming - Functions

Main Ideas of Functional Programming.

  1. Function - This article

  2. Compositions with Functions

  3. Currying

  4. Catamorphism Collapsable

  5. Applicative

  6. Monad Chainable

  7. Monoid Aggregatable

Note:- In the following article, only function concept of functional Programming is introduced. I will be writing a whole series on functional programming from concept to application. Personally i think functional programming is highly useful on the frontend side of the web-development.

With TypeScript, React introducing hooks (which are highly loved by me), Graphql which has a typed Schema for requesting APIs, Testing libraries like Jest.

All of this, which are functional Programming based like declarative (react) , algebraic data typed (graphql schema). I think i should have learnt functional Programming before React.

What we don’t do in FP?

We don't do that here.jpg

In the Practical setting

An Analogy to explain FP

In childhood, We all have played with legos or building-block of some kind. We can make a lot of things with combining these lego pieces, depending upon what we are making. May be you want to make star war ship or iron man mask. The building block i.e. lego are used in some combinations to make a desired model. If a lego piece gets broken you can replace it with a new piece.

Functions are the legos of Functional Programming.

Functional Programming Paradigm has a lot of mathematical terms like monad, function of function or higher order function. Whereas, OOP has a lot of biological terms like polymorphism, inheritance.

Give me a definition.

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 colocated with methods in objects.

Functional code tends to be

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. Not mutating the data. Immutability is a core of functional programming.

What is a Pure Function?

A pure function is a function which:

Hello World Of FP.

const hi = name => `Hi ${name}`

const greeting = hi; 

greeting("Stranger")   //       'Hi Stranger'
       

A simple composition example

const square = x => x*x;

const addition = x, y => x + y;

const SquareRoot = x => Math.sqrt(x);


const EuclideanDistance = x, y => SquareRoot(addition(square(x), square(y)));


console.log(EuclideanDistance(3,4));

UpperCase the first letter of a String

function UpperCase([first, ...rest]){
    return first.toUpperCase() + rest.join('')
}

console.log(UpperCase('dan'))

** if you have any suggestions, please do tell me… **

Sun Feb 18 2024 00:00:00 GMT+0000 (Coordinated Universal Time)