DEV Community

Cover image for Functional Programming : Functions
Pratik sharma
Pratik sharma

Posted on

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

  • 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'

Enter fullscreen mode Exit fullscreen mode

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));

Enter fullscreen mode Exit fullscreen mode

UpperCase the first letter of a String

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

console.log(UpperCase('dan'))


Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
saroamirkhanyan profile image
Saro Amirkhanyan

If you want to function have many arguments you have to put arguments into bracket
const addition = (x, y) => x + y