Functional Programming : Functions

Main Ideas of Functional Programming.

Function – This article
Compositions with Functions
Currying
Catamorphism Collapsable
Applicative
Monad Chainable
Monoid Aggregatable

Note:- In the following article, only func…


This content originally appeared on DEV Community and was authored by Pratik sharma

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

  • No Impure Functions

  • No shared State

  • No Mutable data

  • No Side Effects

In the Practical setting

  • Loops => do...while for for...of for...in
  • Variable declarations with var or let
  • Void functions
  • Object mutation (for example: o.x = 5;)
  • Array mutator methods => copyWithin, fill, pop, push, reverse, shift, sort, splice, unshift

  • Map mutator methods => clear, delete, set, Set mutator methods, add, clear, delete

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

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

What is a Pure Function?

A pure function is a function which:

  • Given the same input, will always return the same output.
  • Produces no side effects.

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... **


This content originally appeared on DEV Community and was authored by Pratik sharma


Print Share Comment Cite Upload Translate Updates
APA

Pratik sharma | Sciencx (2021-04-23T16:23:21+00:00) Functional Programming : Functions. Retrieved from https://www.scien.cx/2021/04/23/functional-programming-functions/

MLA
" » Functional Programming : Functions." Pratik sharma | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/functional-programming-functions/
HARVARD
Pratik sharma | Sciencx Friday April 23, 2021 » Functional Programming : Functions., viewed ,<https://www.scien.cx/2021/04/23/functional-programming-functions/>
VANCOUVER
Pratik sharma | Sciencx - » Functional Programming : Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/functional-programming-functions/
CHICAGO
" » Functional Programming : Functions." Pratik sharma | Sciencx - Accessed . https://www.scien.cx/2021/04/23/functional-programming-functions/
IEEE
" » Functional Programming : Functions." Pratik sharma | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/functional-programming-functions/. [Accessed: ]
rf:citation
» Functional Programming : Functions | Pratik sharma | Sciencx | https://www.scien.cx/2021/04/23/functional-programming-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.