Functional Programming – Counter
- 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)
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
- 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>
(usejava.util.Comparator
for this, don’t create your own class) - Use an inline anonymous class
- Use a lambda expression
- Creating a class that implements
- Use
apple.forEach
to print out all the entries in the list of apples, passing in a lambda expression toforEach
. Note you can also use a method reference in the simple case. - 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>
andComparator<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
- Print all the apples in your list of apples
- Print all the apples in your list of apples, but skip the first 3 entries
- Find the first element in the list and if it’s present, print it
- Filter out apples picked before a certain date and print them
- Filter every apple which has a best before prior a certain date, and print “There is a
colour
apple that is best beforebest before
” - Filter out all red apples, and print “There is a
colour
apple that is best beforebest before
” - Sort the apples by date picked, skip the first three and print the names of the rest
- Use
.collect
to create a list of apples where the apple’s colour contains an e (red
,green
) and print “There is acolour
apple that is best beforebest before
” - 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
.
- Modify
Counter<T>
inCounter.java
so that it uses a Predicate to decide whether an object should be counted or not. - 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
- E.g. could we convert the
- 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?