Functional Programming – Counter

Learning Goals

  • Software design approaches and patterns, to identify reusable solutions to commonly occurring problems
  • Apply an appropriate software development approach according to the relevant paradigm (for example object oriented, event driven or procedural)

Programs Used

Counter

Clone the starter repo here to your machine and follow the instructions in the README to run the app.

Lambda Exercises

The exercises should be added below // Add your lambda exercises here in App.java

  1. Sort the list by best before date using the Collections.sort method. Try out all three of the different methods in the explanation, specifically:
    • Creating a class that implements Comparator<Apple> (use java.util.Comparator for this, don’t create your own class)
    • Use an inline anonymous class
    • Use a lambda expression
  2. Use apple.forEach to print out all the entries in the list of apples, passing in a lambda expression to forEach. Note you can also use a method reference in the simple case.
  3. Create an array of apple comparators: Comparator<Apple>[] comparators = new Comparator[3].
    • Assign each of the 3 comparators with a lambda expression to compare an apple based on date picked, best before date, and colour (alphabetical).
    • Then create a separate method called printApples which takes List<Apple> and Comparator<Apple> as arguments and will sort and display all the apples using the comparator given.

Streams Exercises

The exercises should be added below // Add your stream exercises here in App.java

  1. Print all the apples in your list of apples
  2. Print all the apples in your list of apples, but skip the first 3 entries
  3. Find the first element in the list and if it’s present, print it
  4. Filter out apples picked before a certain date and print them
  5. Filter every apple which has a best before prior a certain date, and print “There is a colour apple that is best before best before
  6. Filter out all red apples, and print “There is a colour apple that is best before best before
  7. Sort the apples by date picked, skip the first three and print the names of the rest
  8. Use .collect to create a list of apples where the apple’s colour contains an e (red, green) and print “There is a colour apple that is best before best before
  9. Use .count to count up how many apples were picked after a certain date

Predicate Exercises

This exercise already has code (located in App.java) which prints counts, using Counter.java.

  1. Modify Counter<T> in Counter.java so that it uses a Predicate to decide whether an object should be counted or not.
  2. Instantiate a counter that counts all apples, and another that counts only red apples

A “Functional” Gilded Rose

There is an established refactoring exercise called The Gilded Rose. We have made a slightly custom version of the exercise for you to complete. The original version is here.

Firstly, fork and clone the starter repo here to your machine and follow the instructions in the README. Your task is to follow a functional approach for rewriting the code. The requirements for the Gilded Rose can be found in your repo in the file gilded_rose_requirements.md.

The repository includes a “golden master” test, which is a simple snapshot test to check that your refactor hasn’t broken anything. It also includes a simple example test that can be used as a pattern if you want to implement more targeted unit tests.

You can run the tests with ./gradlew test.

Things you might want to consider improving:

  • The structure of the Gilded Rose class is very OOP currently, how could we convert that to be fundamentally more functional?
    • E.g. could we convert the update_quality function to be a “pure” function?
    • This may require changing the tests to follow the new pattern
  • Could we split up the handling of the quality & sell_in properties to be separate?
  • Could we reduce (or even eliminate!) any “assignment” operations (x = y) where state is being handled?
  • Could we split the logic to be more readable?