# 2015-01-18-Learning-Scala-Ch5

This post summarises notes and/or exercise solutions of *Chapter 5 First Class Functions* of Learning Scala by Jason Swartz. More complete solutions can be found HERE. Scala code is originally executed in a Eclipse Scala worksheet.

### Notes

#### functions can be treated as data - store in values assigning static type

#### higher-order functions

#### function literals

#### assigning function value vs function literal

#### placeholder (_) syntax

- the explicit type of the function is specified outside the literal and
- the parameters are used no more than once.

#### partially-applied functions and currying

#### by-name parameters can take either a value or a function

- if a function is entered, it’ll be invoked every time that can cause a performance issue

#### partial functions can only partially apply to their input data (eg div by 0)

- error if other than 200, 400, 500 is entered
- possible usage - ‘collect’ every item in a collection that is accepted by a given partial function

#### invoking higher-order functions with function literal blocks

### Exercises

#### 1. Write a function literal that takes two integers and returns the higher number. Then write a higher-order function that takes a 3-sized tuple of integers plus this function literal, and uses it to return the maximum value in the tuple.

#### 2. The library function util.Random.nextInt returns a random integer. Use it to invoke the “max” function with two random integers plus a function that returns the larger of two given integers. Do the same with a function that returns the smaller of two given integers, and then a function that returns the second integer every time.

#### 3. Write a higher-order function that takes an integer and returns a function. The returned function should take a single integer argument (say, “x”) and return the product of x and the integer passed to the higher-order function.

#### 4. Let’s say that you happened to run across this function while reviewing another developer’s code:

#### 5. There’s a function named “square” that you would like to store in a function value. Is this the right way to do it? How else can you store a function in a value?

#### 6. Write a function called “conditional” that takes a value x and two functions, p and f, and returns a value of the same type as x. The p function is a predicate, taking the value x and returning a Boolean b. The f function also takes the value x and returns a new value of the same type. Your “conditional” function should only invoke the function f(x) if p(x) is true, and otherwise return x. How many type parameters will the “conditional” function require?

#### 7. Do you recall the “typesafe” challenge from the exercises in Chapter 3? There is a popular coding interview question I’ll call “typesafe”, in which the numbers 1 - 100 must be printed one per line. The catch is that multiples of 3 must replace the number with the word “type”, while multiples of 5 must replace the number with the word “safe”. Of course, multiples of 15 must print “typesafe”.

Use the “conditional” function from exercise 6 to implement this challenge. Would your solution be shorter if the return type of “conditional” did not match the type of the parameter x? Experiment with an altered version of the “conditional” function that works better with this challenge.