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; ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.