Dev. blogi
Functional programming
Our guest Venkat Subramaniam spoke about functional programming on the JVM, explained basic concepts and gave a good overview of alternative languages for functional programming.
Sysart HotSpot is a small conference/workshop that is held twice a year. Usually there's one speaker, who is an expert on some topic which vary from process to coding, and this time it was pure coding.
This time we had Venkat Subramaniam as our guest. Venkat spoke about functional programming on the JVM, explained basic concepts and gave a good overview of alternative languages for functional programming. The day ended with a workshop where functional programming was used to resolve some basic problems.
Venkat Subramaniam explains functional programming
Functional programming can be described as having the following features:
- Assignment-less programming
- Immutable State
- Functions as first-class citizens
- Higher-order functions
- Functions with no side-effects.
Three of these go more-or-less hand in hand, namely assignment-less programming, immutable state and functions without side-effects. The starting point is immutability. Immutability means that once a value has been set, you cannot change it. For example, when you have a list of things and you add one thing into it, you do not mutate the original list, you get a new list. This might sound inefficient, but immutability has a lot of benefits. Immutability is one of the principles of functional programming. A function should be pure; processing its arguments and returning a result without touching or modifying anything outside it's own scope. A pure function has no side-effects, and when given same parameters, the result is always the same. Assignment-less programming is can force functions to be side-effect free, as no values are assigned into variables which could change the result of the function.
Assignment-less programming, immutable state and functions without side-effects are a good principle in object oriented languages as well. Not a whole lot of objects need to ever alter their state when you come to think about it. Complexity goes down a lot when you use immutable objects as much as you can. Almost everything that information systems do, can be boiled down into a chain of simple, reliable and testable functions. The point here is, immutability is not about the language. Some languages make it (a lot!) easier for you than others, but the language should never be an excuse for not striving for pureness through immutability.
Higher order functions can take other functions as parameters. This requires that functions are treated as first class citizens, i.e. they can be stored in variables and passed around as arguments (just like objects in object oriented languages). This concept is pretty easy to understand, but it isn't so easy to see all the places where it should be used. Simple examples include passing a filter function to another function for filtering lists. But it can be used in a lot of different ways, and probably the only way to learn it is using functional programming.
Functional programming is old paradigm, but previously on academic circles. But as computing becomes more and more parallel, functional programming is one the necessary tools which can be used solve problems related to parallelism.
Kommentoi