Functional Programming 101
4 min read
“Amazing!” that was my initial reaction when I heard and read about functional programming , I am very new to the whole concept so I might go a little off while writing about it so I am open to criticism . This is basically my understanding about functional programming and why I got hooked to it .
Functional Programming is a concept just like Object Oriented Programming , a lot of people confuse these concept and start relating to a particular language , thing that needs to be clear is languages are tools to implement concepts. There is imperative programming where you tell the machine what to do ? For example
Assign x to y
Open a file
Read a file
While when we specifically talk about
FP it is a way to tell how to do things ? The nearest example that I can come up with is
SQL query where you say something like
SELECT * FROM Something where bang=something and bing=something
Here we didn’t tell what to do but we told how to do it. This is what I got as a gist of functional programming where we divide our task into various
functional parts and then we tell how things have to be implemented on the data.
Some of the core concepts that I came across was
pure functions and
functions treated as first class citizen or
first class object . What each term means lets narrow it down .
Pure functions is a function whose return value is determined by the input given, the best example of
pure functions are
Math functions for example
Math.sqrt(x) will return the same value for same value of
x. Keeping in mind that
x will never be altered. Lets go on a tangent and see that how this
immutability of x is a good thing, this actually prevents data from getting corrupt. Okay! That is alot to take in one go, lets understand this with a simple borrowed example from the talk I attended.
We will take example of a simple
Library System now for every library system there should be a book store, the
book store here is an
immutable data structure now what will happen if I want to add a new book to it ? Since it is
immutable I can’t modify it , correct ? So a simple solution to this problem is every time I add or remove a book I will actually deliver a new
book store and this new book store will replace the old one. That way I can preserve the old data because hey we are creating a whole new store. This is probably the gist or pros of functional programming.
book_store = ["Da Vinci's Code", "Angles and Demons", "The Lost Symbol"] def add_book( book_store, book): new_book_store =  map(lambda old_book: new_book_store.append(old_book), book_store) new_book_store.append(book) return new_book_store
print add_book(book_store, "Inferno") # ["Da Vinci's Code", "Angles and Demons", "The Lost Symbol", "Inferno"]
print book_store # ["Da Vinci's Code", "Angles and Demons", "The Lost Symbol"]
In the above code you can actually see that a new
book store is returned on addition of a new book. This is what a pure function looks like.
Function as first class citizens , I can relate a lot to this because of python where we say that everything is a first class objects. So, basically when we say functions are first class citizen we are implying that
functions can be assigned to a variable, passed as a parameter and returned from a function. This is way more powerful then it sounds this bring a lot modular behavior to the software you are writing, it makes the project more organized and less tightly coupled. Which is a good thing in case you want to make quick changes or even feature related big changes.
def find_odd(num): return num if(num%2 != 0) else None
def find_even(num): return num if(num%2 == 0) else None
def filter_function(number_list, function_filter): return [num for num in number_list if(function_filter(num) != None)]
number_list = [1,2,3,4,5,6,7,8,9] print filter_function(number_list, find_odd) # [1,2,5,7,9] print filter_function(number_list, find_even) # [2,4,6,8]
In the above code you can see that function is passed as an argument to another function.
I have not yet explored into
lambda calculus which I am thinking of getting into . There is a lot more power and beauty in functional programming. I want to keep this post a quick read so I might cover some code example later, but I really want to demonstrate this code.
def fact(n, acc=1): return acc if ( n==1 ) else fact(n-1, n*acc)
acc=1 this is
pure textbook and really beautiful code which calculates factorial of
n , when it comes to
FP it is said
To iterate is Human, to recurse is Divine. I will leave you to think more about it, will try to keep writing about things I learn.
Did you find this article valuable?
Support Farhaan Bukhsh by becoming a sponsor. Any amount is appreciated!