Last updated on
Find, with a higher-order function
In the context of our labs, a callback is a small modification to a previous lab that uses a newly introduced concept to simplify the code. In this week’s callback, you will re-implement different find*
functions from the Find lab using a higher-order function.
Obtaining the lab files
Run the following commands to obtain a copy of the lab:
$ git clone https://gitlab.epfl.ch/lamp/cs-214/find-hof.git
$ cd find-hof
The provided code contains possible solutions to the previous labs. You will need to modify the find.scala
file to complete this lab.
Step 1: Implement findAndPrint
Add a findAndPrint
function in find.scala
.
This function should take two arguments:
- a root
cs214.Entry
to search in, - a predicate function that takes an
cs214.Entry
and returns aBoolean
.
The function should print the path of all entries in the tree that satisfy the predicate, and—like your existing find*
functions—return true
if any entry was found and false
otherwise.
Step 2: Rewrite the find*
functions
Rewrite findAllAndPrint
, findByNameAndPrint
, findBySizeEqAndPrint
, findBySizeGtAndPrint
and findEmptyAndPrint
using findAndPrint
. Each function should call findAndPrint
with the appropriate predicate function.
You do not need to edit findFirstByNameAndPrint
, because it cannot be elegantly implemented using findAndPrint
. We will come back to this function in a future callback!
Testing
As refactoring the code should not change the behavior of the program, the test suite FindTest
is exactly the same as the one from the Find lab.
In addition, there is a new test suite FindHOFTest
that checks that you have indeed added the findAndPrint
function and that you have rewritten the 5 find*
functions mentioned above using it. This is the test suite that will be used to grade this callback. You do not need to understand how this test suite works.
You can run all suites as usual with:
$ sbt test
Submit your patch!
Once your are done, you should submit your code on Moodle. Since we are revisiting old code, rather than writing new code, the submission is done as a patch.
-
If you forgot how to create patches, you can re-watch the online lecture, or redo the introductory exercises on patching.
-
Select files to commit using
git add
. Make sure to select only relevant files. In this assignment, you should be including exactly two files,src/main/find/find.scala
andsrc/main/HowManyHoursISpentOnThisLab.scala
. -
Save your work using
git add
, followed bygit commit
, with the right parameters. Make sure that your commit message if correctly formatted. We enforce the following format:<Scope>: <Summary> <Description>
- The scope indicates what part of the course the code relates to.
In this assignment, the scope should be
find-hof
. - The summary gives a high-level overview your changes. It should start with a capital letter.
- The description describes the changes in detail—this includes information such as why the changes were necessary, which functions were changed, or what implementation decisions were taken. Here are some examples.
- The complete header line (scope + summary) should be at most 80 characters.
- Note that the
<
and>
symbols are often used to show places where you should fill in a value: do not actually write these symbols in your message!
A plain call to
git commit
will open your$GIT_EDITOR
, which lets you input the header line and the body line separately. If you prefer to stay on the command line, you can pass the-m
option multiple times:git commit -m "<Scope>: <Summary>" -m "<Description>"
. - The scope indicates what part of the course the code relates to.
In this assignment, the scope should be
-
Create a patch from your commit, using
git format-patch
with the correct arguments, and rename it tolab.patch
. -
Submit your patch (named
lab.patch
) to Moodle.
If you run into trouble with the grader, do not modify the patch file by hand. Instead, simply use git commit --amend
to fix up your last commit, then regenerate the patch.
If you need help, review the lecture about version control and the corresponding exercise set.