WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Intermediate · question 39 of 100

How can you create and use custom control structures in Scala?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

In Scala, you can create custom control structures by defining methods that take function literals as parameters. These functions can then be called with code blocks. This allows you to create flexible and reusable control structures to maintain clean and easy-to-understand code.

Here’s a step-by-step example of creating and using a custom control structure in Scala:

1. Define the custom control structure as a method that takes a function parameter.

Let’s create a custom control structure called ‘twice‘ that takes a code block and executes it two times:

def twice(block: => Unit): Unit = {
  block
  block
}

Here, ‘block‘ is a function parameter of type ‘=> Unit‘, which means it is a parameterless function that returns ‘Unit‘. The ‘=>‘ syntax allows you to pass code blocks without needing to explicitly convert them into a function.

2. Use the custom control structure with a code block.

Now you can use the ‘twice‘ control structure to execute a code block two times:

twice {
  println("Hello, world!")
}

This will produce the following output:

Hello, world!
Hello, world!

The ‘twice‘ control structure can be used with any code block, making it a flexible and reusable component in your code.

3. Custom control structures with parameters and result types.

You can also create custom control structures that take additional parameters or return a result type. Here’s an example of a custom control structure ‘repeat‘ that executes a code block a specified number of times and returns a list of the results:

def repeat[A](n: Int)(block: => A): List[A] = {
  if (n <= 0) Nil
  else block :: repeat(n - 1)(block)
}

The ‘repeat‘ method takes two parameter lists: the first one contains an ‘Int‘ parameter ‘n‘ that specifies the number of repetitions, and the second one contains a function parameter ‘block‘ of type ‘=> A‘, where ‘A‘ is a type parameter.

You can use the ‘repeat‘ control structure with different types of code blocks:

val numbers = repeat(3) {
  scala.util.Random.nextInt(100)
}
println(numbers)

repeat(2) {
  println("Hello, world!")
}

In this example, ‘numbers‘ will be a ‘List[Int]‘ containing three random integers, and "Hello, world!" will be printed two times.

In conclusion, custom control structures in Scala provide a powerful way to create reusable and expressive components in your code. By taking function literals as parameters, you can design flexible control structures that work with a variety of code blocks.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics