Introduction to Functional Programming

Keywords: Programming Lambda JDK

What is Functional Programming

Functional programming is a programming paradigm that treats computer operations as calculations of functions.The most important basis of a functional programming language is the lambda calculus.Moreover, functions of the lambda operator can accept functions as inputs (parameters) and outputs (return values).Functional programming emphasizes that function calculation is more important than instruction execution than instructional programming.Compared with procedural programming, in functional programming, the calculation of functions can be called at any time.

Functional programming in Guava

To support functional programming, Guava provides the following two interfaces:

public interface Function<F, T> {
    @Nullable T apply(@Nullable F input);
}
public interface Predicate<T> {
    boolean apply(@Nullable T input);
}

For example, in a group of People objects, find a People older than 20

  • Common practice:
    List<Person> oldPeople = new ArrayList<Person>(); 
    for (Person person : people) { 
    if (person.getAge() >= 20) { 
        oldPeople.add(person); 
    }
    }
  • A filter pattern is provided in Guava, where elements are filtered from a set based on a condition.
    • Find an age greater than 20
      List<Person> oldPeople = Lists.newArrayList(Collections2.filter(people, new Predicate<Person>() { 
      public boolean apply(Person person) { 
          return person.getAge() >= 20; 
      } 
      }));
    • Find the name that contains the character w
      List<Person> oldPeople = Lists.newArrayList(Collections2.filter(people, new Predicate<Person>() { 
      public boolean apply(Person person) { 
          return person.getName().contains("w");
      } 
      }));
    • Find the person whose age is > 20 and whose name contains the character w
      // Usually everybody writes like this, of course this is OK
      List<Person> oldPersons = Lists.newArrayList(Collections2.filter(persons, new Predicate<Person>() {
      public boolean apply(Person person) {
          return person.getAge() >= 20 && person.getName().contains("w");
      }
      }));

      There will be a certain amount of code duplication if you write this because we have written two Predicates to implement these two conditional judgments separately. Can you reuse the previous Predicate?The answer is yes.We first extract the two Predicates that previously generated age and name judgments into methods.

      private Predicate<Person> ageBiggerThan(final int age) { 
      return new Predicate<Person>() { 
          public boolean apply(Person person) {
              return person.getAge() >= age;
          }
      }; 
      } 
      private Predicate<Person> nameContains(final String str) { 
      return new Predicate<Person>() { 
          public boolean apply(Person person) { 
              return person.getName().contains(str); 
          }
      };
      }
      //And our result is that these two Predicates actually match.Guava provides us with an and method for summing a set of Predicates.
      List<Person> filteredPeople = Lists.newArrayList(Collections2.filter(people, Predicates.and(ageBiggerThan(20), nameContains("w"))));

Functional programming in JDK8

In the example above:

  • In a group of People objects, find a People older than 20
    List<Person> filteredPeople = persons.stream().filter(p -> p.getAge() >= 20).collect(Collectors.toList());  
  • Find the name that contains the character w
    List<Person> filteredPeople = persons.stream().filter(p -> p.getName().contains("w")).collect(Collectors.toList());  

summary

  1. Unable to upgrade jdk version of project, guava is recommended > guava is worth using as the best alternative to jdk8
  2. JDK projects can be upgraded smoothly, jdk8 is recommended > jdk8's natural advantage, bound to be superior to guava, plus the lambda expression introduced later, makes the syntax of jdk8 more graceful
  3. Jdk8 was born after guava, so many parts of jdk8 are based on guava

Posted by MichaelHe on Fri, 28 Jun 2019 09:35:02 -0700