Skip to content

Instantly share code, notes, and snippets.

@JonNorman
Last active October 24, 2018 12:18
Show Gist options
  • Save JonNorman/9d1d8905607ec19fc8b66b71a9ac964c to your computer and use it in GitHub Desktop.
Save JonNorman/9d1d8905607ec19fc8b66b71a9ac964c to your computer and use it in GitHub Desktop.
Exercises for Scala School, starting to try and tie together everything we have covered so far.

Week 4 Exercises

These exercises are intended to bring together a lot of what we have covered so far in our lessons. Make sure you ask for help on the scala-school channel if you get stuck!

There will be a part-2 of these exercises that will look at expanding our domain to include Books and Music, which will explore traits and how we can use them.

Remember that there are the recap worksheets that you can use as references and cheat sheets whilst attempting the exercises.

Scenario

Flying in the face of successful subscription-based services, let's start mapping out the domain of an entertainment website where people can rent items of interest, we can call it Guflix and we can start with Films.

Exercises

Exercise 1

Model a Film with a case class. It should have a title, director, runtime (in minutes), releaseDate, rating, price (in £), and genre.

Hints:

  • Think about what types these fields should be. You could use a String for your releaseDate field, but you could try importing java.util.LocalDate and using that instead.

  • The names of case classes and objects are always TitleCase whereas fields are always camelCase.

  • Once you've defined your case class, try creating a few instances with different values and storing them in separate variables or directly in a List[Film].

Exercise 2

Create a Film object (as a convenient place to store functions) and define the following functions within it:

  1. a function called filmsWithinBudget that takes in a List of Films and a budget and returns a List of Films that can be rented within that budget.

  2. a function called withStandardiseDirectorName that takes a Film and returns a Film where everything is the same apart from the director name, which been converted to a first initial followed by the surname e.g. Martin Scorsese -> M. Scorsese Kathryn Bigelow -> K. Bigelow The Coen Brothers -> The Coen Brothers How might you handle the latter case? How might you create a copy of the input Film and change the value of the director field?

  3. a function called standardiseDirectorNames that takes a List of Films and returns a List of Film where all of the director names have been standardised. Try to reuse what you have already written.

  4. a function called findSimilar with the following signature:

def findSimilar(mainFilm: Film, candidates: List[Film]): List[Film]

it should return a List of Films that are 'similar' to the mainFilm argument. You can decide what 'similar' means:

  • maybe the runtimes are close and the genre is the same;
  • maybe the director matches and rating is the same;
  • maybe the releaseDates are less than 3 months apart; or
  • maybe a mix of all of those!

Hints:

  • Create some instances of your Film case class and test that your functions are behaving correctly. In a future class we will look at writing tests properly!
  • If you want to do something generic with a type e.g. uppercase a letter, split a String into a List[String] according to a delimiting character, the chances are that a method or methods will already exist for doing that. Have a look around at methods on that type either by using IntelliJ or by looking at the documentation e.g. String: https://www.scala-lang.org/api/current/scala/collection/immutable/StringOps.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment