The Rise of Functional Programming
Functional programming (FP) used to be an obscure paradigm that few software developers had heard of but today almost every major programming language - Java, C#, Python, Javascript - has borrowed concepts or implemented features from FP to make development faster and easier.
Functional programming is a programming paradigm for developing software using functions only.
Following the FP philosophy means avoiding things like shared states, mutable
data and side effects. Unlike procedures that depend on a local or global state,
outputs in FP depend only on the arguments passed to the function. Taking out side
effects, or state changes that don't depend on function inputs, makes software
behave more predictably, which is a major selling point for many FP coders.
Side
effects: Side
effects are any state changes that occur outside of a called function aside
from the returned value like modifying a global variable, printing onto
console.
Higher order functions: Higher
order functions are the functions that take other functions as arguments and
they can also return functions. The higher order function map() offers a better
alternative to iterating over lists. For example, this simple map written in
Python takes a list of names and returns the character lengths of those names:
name_lengths = list(map(len, ["Bob",
"Rob", "Bobby"]))
print(name_lengths) #
[3, 3, 5]
Here the function "map" takes
in another function "len" as the first argument and a list as the second argument. Then it applies the
function over each element of the list to produce another list which gives us
the length of each name.
The non-functional approach would look like this:
name_lengths = []
for name in names:
name_lengths.append(len(name))
print(name_lengths)
Notice that even for this simple
program the functional approach took 2 less lines of code to write. As programs
become more complex in logic functional approach becomes that much easier to
write.
import functools
fib = lambda n: functools.reduce(lambda x,y: (x[1], x[0] +
x[1]), range(n), (0, 1))[0]
print(fib(5)) #5
print(fib(8)) #21
The above program defines a one line function
fib that prints the n-th fibonacci number using the higher-order function
reduce. Try to figure out how it works by looking up how the reduce function
works in python!
Declarative paradigm: Functional programming is a declarative paradigm, meaning
that the program logic is expressed without explicitly describing the specific
steps used to achieve the desired results. Declarative programs abstract away the how and focus on the what.
SQL, for instance, uses a declarative approach. You do not specify how to get
the tuples and attributes that you want. You simply write:
SELECT id, name FROM table. The inner details of how that is achieved is
abstracted away.
The opposite approach is imperetive
where you specify how to get what you want. Most traditional programming
languages use this approach, like C, C++, Java. Here you have to specify
procedurally how to achieve what you want.
With this you
have a general idea of what FP is all about. If you want to learn more about FP
you may try your hand at Haskell. There are some online resources available
that may help you:
https://www.tryhaskell.org/
http://learnyouahaskell.com/chapters
Credits:
Author: Abhishek
Bhattacharya
Github: https://github.com/ShinodaII
LinkedIn: https://www.linkedin.com/in/abhishek-bhattacharya1997/
We will be posting amazing blogs here. Keep following us -
YouTube - youtube.com/techStormers
Facebook - facebook.com/techstormers
Instagram - instagram.com/techstormers
Comments
Post a Comment