Most functional languages have a concept of a lambda function. It is an anonymous function defined inline. It can be assigned to a variable if needed. For example, consider that we need a function that accepts a cookie with user session data, in the context of a web application. Its job is to print a greeting to the user to the standard output. However, before printing, we need to decorate the user's name in a certain way. To complicate matters further, we also have a database of users who hold PhDs and, if they do, we need to refer to them as Dr. Here is how it can be done in Scala:
- We define the dummy Cookie class for our example:
case class Cookie(name: String, gender: String)
- We define the greeting method. The job of the method is to extract the data from the cookie object, and apply the modifier to the user's name based on their...