From 69f405d7a0ac6a7d33f09918f808432fcc49a1c3 Mon Sep 17 00:00:00 2001 From: "matt.venz@zendesk.com" Date: Tue, 10 Oct 2023 11:00:49 +1100 Subject: [PATCH] Intro exercises --- .../introcourse/level01/IntroExercises.scala | 78 +++++++++---------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/src/main/scala/introcourse/level01/IntroExercises.scala b/src/main/scala/introcourse/level01/IntroExercises.scala index 444f5664..842058d2 100644 --- a/src/main/scala/introcourse/level01/IntroExercises.scala +++ b/src/main/scala/introcourse/level01/IntroExercises.scala @@ -1,7 +1,6 @@ package introcourse.level01 -/** - * Level 1 focuses on basic Scala, including syntax and especially programming with functions. +/** Level 1 focuses on basic Scala, including syntax and especially programming with functions. * We will go through a few exercises to familiarise ourselves with writing Scala. * * What's a function? @@ -11,92 +10,87 @@ package introcourse.level01 */ object IntroExercises { - /** - * This function called `add` takes two Ints and return an Int. + /** This function called `add` takes two Ints and return an Int. * You must specify the types of the inputs but the output return type is optional * and can be inferred by the compiler. * scala> add(1, 2) * > 3 - **/ - def add(x: Int, y: Int): Int = ??? - + */ + def add(x: Int, y: Int): Int = x + y - /** - * Let's write the curried version of the `add` function defined previously + /** Let's write the curried version of the `add` function defined previously * scala> addCurried(1)(2) * > 3 - **/ - def addCurried(x: Int)(y: Int): Int = ??? - + */ + def addCurried(x: Int)(y: Int): Int = x + y - /** - * Reuse the `addCurried` function and partially apply it for adding 5 to anything. + /** Reuse the `addCurried` function and partially apply it for adding 5 to anything. * scala> add5(4) * > 9 - * - **/ + */ def add5(x: Int): Int = { - val f: Int => Int = ??? - ??? + val f: Int => Int = addCurried(5) + f(x) } - /** - * Parametric types + /** Parametric types * * How many ways can you implement this function? * Note: Square brackets (Types at compile time), round brackets (Values at run time) */ - def foo[A](a: A): A = ??? + def foo[A](a: A): A = a - /** - * How about this one? + /** How about this one? */ - def bar(a: Int): Int = ??? + def bar(a: Int): Int = 1000 - /** - * What does the return type of this function tell us about + /** What does the return type of this function tell us about * what it can do once implemented? */ def pandora(x: Int): Unit = ??? - /** - * scala> timesTwoIfEven(4) + /** scala> timesTwoIfEven(4) * > 8 * scala> timesTwoIfEven(3) * > 3 * * Important: Every `if` must have an `else`! Otherwise your function is not total. */ - def timesTwoIfEven(x: Int): Int = ??? + def timesTwoIfEven(x: Int): Int = { + if (x % 2 == 0) x * 2 + else x + } - /** - * scala> showNumber(100) + /** scala> showNumber(100) * > "The number is 100" * * Hint: Use string interpolation, e.g. s"$x" */ - def showNumber(x: Int): String = ??? + def showNumber(x: Int): String = s"The number is $x" - /** - * Tuples + /** Tuples * * How can we group together `name` and `age` in a pair? */ - def pair(name: String, age: Int): (String, Int) = ??? + def pair(name: String, age: Int): (String, Int) = (name, age) - /** - * How can we extract the first element of a pair? + /** How can we extract the first element of a pair? * * Hint 1: Check the getters on `pair`. * Hint 2: Read up on pattern matching on tuples. * * https://docs.scala-lang.org/tour/tuples.html */ - def first(pair: (String, Int)): String = ??? + def first(pair: (String, Int)): String = { + val (name, _) = pair + name + } - /** - * How can we extract the second element of a pair? + /** How can we extract the second element of a pair? */ - def second(pair: (String, Int)): Int = ??? - + def second(pair: (String, Int)): Int = { + pair match { + case (_, age) => age + } + } }