Functional Programming – Just so you know

Published by Rienz Otiong on

This is my first blog for the series called “Just so you know” where I will talk about things that you might already know or not. So for now we going to talk about Functional Programming.

 

 

What is Functional Programming?

a programming paradigm just like OOP where you have objects with methods in them, Structured Programming, Procedural and more however Functional Programming is where functions is the main character. Basically functional programming provides pure function and avoids things like mutable data or side effects. so most likely the data enters as an input and the function will provide an output without affecting any variables.

 

 

Why Use Functional Programming?

  1. Easier to understand.
  2. Easier to debug.
  3. Reduce duplicate codes.
  4. Easier to test using testing scripts.
  5. Easier to predict its output.
  6. Avoid unwanted variable value changes.

 

So know we know the basic meaning of Functional Programming. Let’s see an example implementation.

Not Functional

Functional

 

 

Pure Function

Functional Programming takes an input and use only that given input to provide an output which produces no side effects.

Side Effects  is an effect where it uses an external variable that is not within function or calling an external process like triggering a file edit or printing on the display. Pure Functions will make you avoid any unexpected output or unwanted process.

 

Not Functional (Impure Function)

Functional (Pure Function)

 

 

Higher Order Function

Higher Order Function is a function within a function or can take functions as inputs and/or output.

is same as

the old way

 

 

Functional Methods (map, reduce, filter)

We might iterate an array using for loops and while loop however in a functional way to do it is use map, reduce and filter which returns a new array and doesn’t modify the original array.

So first map transform each item of the array which returns the same length of items. For example

reduce on the other hand returns a data using all of the items resulting a single output value.

lastly filter where it checks every item and removes all of the items that failed on the given condition.

 

 

Immutable Data

This is a continuation of Functional Methods on why we need to use it. Immutable Data is a data that can’t be change which enables us to debug easier since your confident that none of your methods or functions will change the value of your data. This avoids the fact that sometimes we don’t know where our data changes its value since numerous functions use and transform the value of that variable.

Mutable Data

Immutable Data

 

 

For more “Just so you know” posts. Just click here


3 Comments

yue · January 17, 2019 at 10:49 am

Hello, I will continue to pay attention to your dynamics.

    admin · January 17, 2019 at 10:59 am

    Hey dude, Sure thanks I appreciate that.

JavaScript Random Facts - typeof null is object - Codedam · February 18, 2019 at 11:26 am

[…] could check out my other post Functional Programming where I somewhat mention about mutation and how to avoid […]

Leave a Reply

%d bloggers like this: