Homework 3: Complex Kotlin

Due April 10

In this homework assignment, you task is to ensure that the Kotlin code you will be writing will pass all of our given unit tests.

The benefit of completing this assignment is that you will learn how to use the Terminal to write commands to execute a Kotlin script file. At some point in your career as a developer, you'll encounter the command line interface (CLI) as part of your development workflow. Completing this assignment is a good chance to become more comfortable working in such environment.

Always refer to the Assignment section for any changes to the assignment.

Prerequisites

In order to run your test script, you must ensure that the following items have been completed:

Running your test script

You can do so by executing the command kotlinc -script main.kts in your Terminal.

Higher-Order Functions

Kotlin functions can be stored in variables and data structures or even passed down as arguments. This feature is known as first-class function.

Here's an example of a higher-order function:

fun compare(a: String, b: String): Boolean = a.length < b.length

To learn more about higher-order functions and lambdas, read this.

Wikipedia also describes higher-order function in detail, and does so very well. I encourage you to have a go at it too.

Lambdas / Anonymous Functions

Here's a simple lambda expression:

val sum = { x: Int, y: Int -> x + y }

You can also simplify your code like so:

val sum: (Int, Int) -> Int = { x, y -> x + y }

To learn more about higher-order functions and lambdas, read this.

Going Beyond

Tips #1

The special thing about higher-order functions and lambda expressions is that you can use things like .map {} and .filter {}. These higher-order functions will be very useful for your homework assignment.

Feel free to check out this link to learn more about how these types of functions work.

Tips #2

You might find that knowing the difference between listOf(12..20) and listOf(12,13,14,15,16,17,18,19,20) and just plain (12..20) can be very useful.

Tips #3

What exactly is a FizzBuzz puzzle?

Tips #4

repeat(x) function might come in handy whenever you want to repeat, say, a string!

Tips #5

Enumerators and the .invoke() function!

Tips #6

Ask yourself why use .fold() and not .reduce()?

Last updated