Functional programming…

Miki Lee
3 min readAug 12, 2019

--

Taken from pexels.com

Functional programming is a new programming style which replaces object oriented programming (OOP) style.

The fundamental concepts we encounter when learning functional programming are declarative programming, pure functions, and immutability, which eventually are intended to reduce mutation and side effects of application states as much as possible.

First of all, a functional programming pursues a declarative programming style which focuses on more how to compose functions to implement necessary logic by abstracting the internal processes. In contrast to declarative programming, an imperative programming focuses more on how to perform a task in detail, for instance, looping through array. The representative declarative programming examples are SQL and html. We never need to know how commands of SQL and html work, but we are very familiar with what we expect from those commands, which is functional programming aims for.

Following is an example to compare imperative and declarative programming. Specifically, these are codes to read a list of names, normalize them, remove any duplicates, and sort the final result (taken from the book Functional programming in Javascript). The first version is an imperative programming while the second version is a declarative programming.

Imperative programming style (copied from Functional programming in Javascript)
Declarative programming style (copied from Functional programming in Javascript)
Pure function by using Lodash ( _ ) library

Pure function is defined as a function that has input and output and output is always same given same input. So, any random or date function can’t be pure function because the output can’t be same given same input. Pure function can enable developers to create more testable and refactor-able application because it does not depend on and mutate application’s other states other than input. Therefore, it is much less likely to cause application’s side effects because it avoids mutating application’s global states. These days, there are some popular libraries that are supportive of pure functional style programming such as Lodash. The # 3 accesses the global values array while # 5’s map function accepts two arguments (array, callback function) and don’t change any global states.

Immutability is a functional programming style which never mutate variables. There are two different types of variable, primitive and reference type. The primitive type never mutates original values while reference type does it. Specifically, object is a reference type of variable, the reference (memory address) of a defined object is never changed, only actual value of that object is changed. But if we use immutable data type for object, the reference of object is changed. Therefore, by only comparing references, application can recognize the object’s change (shallow comparison). That is much efficient comparison method compared to deep comparison which compares every single values of object. In summary, if we stick to immutable data type, application’s state can be easily detected and debuggable.

The most representative functional programming style functions are map, filter and reduce. Those functions do not loop through array elements one by one even though those are functions are applied to array, which is explained in the declarative programming. Here, I used Lodash library( _ ), all functions are solely dependent on input and return same output given same input.

Functional programming style functions map, filter and reduce

Functional programming is a new style of programming which mimics mathematical function. If we create pieces of functions with functional programming style, how to assemble and compose those functions into application would be important question which I need to dig into more.

--

--