> For the complete documentation index, see [llms.txt](https://uw-info448.gitbook.io/info-448-android-development/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://uw-info448.gitbook.io/info-448-android-development/homework/homework-3-complex-kotlin.md).

# 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.

{% hint style="warning" %}
Always refer to the Assignment section for any changes to the assignment.
{% endhint %}

## Prerequisites

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

* [ ] Make sure you are inside the directory in which your `main.kts` file is stored.
* [ ] You have a compiler that can read and execute Kotlin scripts.

## 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`](https://en.wikipedia.org/wiki/First-class_function) function.

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

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

To learn more about higher-order functions and lambdas, read [this](https://kotlinlang.org/docs/reference/lambdas.html).

{% hint style="info" %}
Wikipedia also [describes higher-order function in detail](https://en.wikipedia.org/wiki/Higher-order_function), and does so very well. I encourage you to have a go at it too.
{% endhint %}

## Lambdas / Anonymous Functions

Here's a simple lambda expression:

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

You can also simplify your code like so:

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

To learn more about higher-order functions and lambdas, read [this](https://kotlinlang.org/docs/reference/lambdas.html).

## 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.&#x20;

Feel free to check out this [link](https://blog.kotlin-academy.com/programmer-dictionary-higher-order-function-9cadb07df94e) 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?](http://wiki.c2.com/?FizzBuzzTest)

### Tips #4

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

### Tips #5

[Enumerators](https://kotlinlang.org/docs/reference/enum-classes.html) and the [`.invoke()`](https://kotlinlang.org/docs/reference/operator-overloading.html#invoke) function!

### Tips #6

Ask yourself [why use `.fold()` and not `.reduce()`](https://stackoverflow.com/questions/44429419/what-is-basic-difference-between-fold-and-reduce-in-kotlin-when-to-use-which)?
