Bastard, following up on what Shuggy said, I think the main concepts are simple and natural. When learning FP, keep in mind that functions are ubiquitously useful and natural abstractions.
Very often I'll find myself doing something like this in Java:
Code:
List<Person> people; // initialized somehow
List<String> phoneNumbers = new ArrayList<String>();
for (Person person : people)
phoneNumbers.add(person.getPhoneNumber());
And then I'll find myself doing this elsewhere:
Code:
List<Person> people; // initialized somehow
List<String> emails = new ArrayList<String>();
for (Person person : people)
emails.add(person.getEmail());
You see the pattern: we take a list of things and create a new list where each element is the result of applying some "transformation" (such as calling getEmail()) to an element of the original list. Unfortunately, Java gives me no elegant way to abstract this pattern out and make a generic method to do this (you could do it but with a lot of unnecessary braces).
What would be more natural is to write something like this once:
Code:
<A, B> List<B> map(Function<A, B> transformation, List<A> items) {
List<B> mappedItems = new ArrayList<B>();
for (A item : items)
mappedItems.add(transformation.evaluate(item));
return mappedItems;
}
Then I could use it in many situations like:
Code:
map(Person.getEmail(), people)
The map "function" condenses 3 lines of code into a simple expression.
----------------
Another situation I often find myself in is computing an aggregate value from a collection:
Code:
Collection<Integer> numbers;
int sum = 0;
for (int number : numbers)
sum += number;
Code:
Collection<Integer> numbers;
int product = 1;
for (int number : numbers)
sum *= number;
Now we want to abstract out two things: the initial value (0 for sum, 1 for product) and the operation for combining each new value (+ for sum, * for product). So we want a general procedure to "fold" values one at a time into an aggregate value, like:
Code:
<A, B> <B> fold(B aggregate, Function2<A, B, B> operation, List<A> items) {
for (A item : items)
aggregate = operation.evaluate(item, aggregate);
return aggregate;
Now, it might be nice to define sum and product operations in terms of fold:
Code:
let sum(numbers) = fold(0, +, numbers)
let product(numbers) = fold(1, *, numbers)
----------------
Both of these examples, map and fold, are "higher order functions": functions taking functions. But they aren't at all complex or scary. They're simple and useful. They can even be combined easily: to get the total popularity of a collection of people, just say something like:
Code:
sum(map(Person.popularity(), people))
The equivalent Java code would be 5 times as long.
At its core, functional programming really is that simple. There are more advanced features in many functional languages, such as macros, monads, etc. More importantly, the extreme power of functions as an abstraction tool can lead to code that's surprisingly complex given its size. However, more often than not, the code is just a composition of simple features, and by mentally breaking it down, it can be understood. (On the other hand, spaghetti procedural code can be extremely hard to break down as each component often relies on side effects of other components.)