O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

It’s a Way of Thinking

So to a first approximation, functional programming is about programming with pure functions and immutable state, unlike imperative programming, which relies heavily on mutability. Around this immutable core there is a set of language techniques and features that replace mutable imperative techniques. Examining these will give us a deeper feeling for what it is to think and program functionally.

Let’s take a look at a simple example: filtering a list so it only contains odd numbers.

 public​ List<Integer> filterOdds(List<Integer> list) {
  List<Integer> filteredList = ​new​ ArrayList<Integer>();
 
 for​(Integer current : list) {
 if​(1 == current % 2) {
  filteredList.add(current);
  }
  }
 return​ filteredList; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required