#higheroderfunctions
Explore tagged Tumblr posts
Text
The rise of Functional Programming languages
When I was starting college, the language used to teach the first computer science class was SML or Standard ML. It was a simple but powerful language which was strongly functional. But unfortunately I did not know what a functional language was back then. Soon the classes moved to the more main stream imperative languages like C, C++ and Java and I ignored the functional programming paradigm. Fast forward a years of working and functional programming is hard to ignore.
It is not the latest rage in programming but definitely an on-going development. Functional Programming languages are being adopted at an ever increasing pace and are considered part of "modern languages" at times. Functional programming has its roots in lambda calculus and Lisp. It was mostly academic for a long time and only in last decade has it began getting used in commercial software development.
Functional programming is a programming paradigm. Hence a lot of languages which are not designed to be functional first can be used to implement functional concepts. Languages like Scala live in the area between functional and imperative languages. F#, for instance, lives on the other extreme and strongly pushes for functional paradigm. All the most profound languages, C++, Java have been getting functional features in their latest upgrades.
Functional programming has a few primary features. First and foremost being functions as first class citizens. That usually translates into being able to define functions anywhere in code and being able to pass them around as you would any other value. For example:
def addTwo(number): return number + 2 def addFunction(number, fn): value = fn(number) return value print(addFunction(5, addTwo)) # returns 7
Second biggest feature for me would be pure functions. Pure functions are functions without any side effects. A simple example of no side effect vs side effect would look something like this:
class Number: def __init__(value): self.value = value def addTwo(number): number.value = number.value + 2 def addTwoNoSideEffect(number): value = number.value + 2 return Number(value)
Functional programming languages are usually also accompanied with higher order functions out of the box. This usually makes most code bases smaller and that is also one of the reason the industry is seeing a movement towards functional languages. For instance, if I want to multiply all the numbers in a list by 2:
nums = [1, 2, 3, 4] #imperative for i in range(len(nums)): nums[i] *= 2 #functional nums = map(lambda x: x*2, nums)
Such higher order functions are also a reason why some of the functional languages are used for scientific purposes in terms of data manipulation and churning. All these come together to make a powerful combination. But there are some drawbacks too. It is important to know the pros and cons of a choice before making it. To simply note, the no side effect way of doing things can lead to temporary high memory usage. If that is not allowed for your application, maybe if it has extensive memory constraints, then functional programming might not be the correct choice. There are other systematic choices, some handled by compiler optimizations and hence not that impact full and some are more obvious as mentioned above.
I have been working with functional programming lately with a combination of Scala, Java8, Python and F#. Some are more functional than others but all have the feature set to allow for functional first code. For any developer, I highly recommend checking out functional programming even if your language of choice is not functional first. As everything, knowledge only helps guide our future decisions.
2 notes
·
View notes