Noppanit

20 Jan 2014

# Thinking functionally for idiots.

I just want to write this post for myself and lucky people who found this post from Google. The world has come to the era of functional. When I pair with my colleagues I always hear “That’s not so functional” or “Should we use Guava or functional Java” or “Wouldn’t it be easier if we use Scala”?. What do these mean? Why should I have to care if my code looks functional and what benefits do I get. This post is going to be a really easy post to follow that’s why the title “Thinking functionally for idiots”. If you want more technical I highly recommend this post from Neal Ford.

Why functional programming?
There are a few concepts of functional programming but you can find the full concepts here. I don’t really want to explain about higher-order or first-class functions because someone else can explain it better than myself. In summary, I really think the benefits of doing functional programming is you tend to avoid bugs and the code might be more readable. These can be achieved by avoiding state and mutable data which most of the sites seem to refer to these two benefits. I would like to show this in a simple example.

List<Integer> numbers = Arrays.asList(1, 2 ,3, 4, 5, 6);

ArrayList<Integer> multipledByTwo = new ArrayList<Integer>();

for(Integer number : numbers) {
}


You can see that this piece of code is really simple and what it’s doing is just multiply every element of the list by two. What’s wrong with this piece of code? First of all, multipledByTwo list acts like a temporary list which holds the answer and programmer can do something else with it.

Let’s see another example. This is the modified version to be more functional.

// Interface
public interface Function<S, T> {
T apply(S in);
}

// MappingClass
public abstract class MappingClass<S, T> {
protected List<T> map(List<S> list, Function<S, T> fun) {
List<T> result = new ArrayList<T>();
for(S item : list) {
}
return result;
}
}

// To Test

public List<Integer> test() {
List<Integer> numbers = asList(1, 2, 3, 4, 5, 6);
List<Integer> results = map(numbers, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return input * 2;
}
});

return results;
}


We create one generic function which is the interface Function this will represent higher-order function which we can parse to another method. Then we create a recursive method which will execute the function and put in another list. This way we don’t create mutable object and we don’t retain any state. What comes in the function comes out as the results.

What do you think? Is this better than the other? If it’s Java I highly doubt that. Java is still not mature enough to do functional programming. But things are looking better in Java 8.

Til next time,
noppanit at 00:00