Skip to content

Commit

Permalink
Intro exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvenz committed Oct 10, 2023
1 parent 3ee27a0 commit 69f405d
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions src/main/scala/introcourse/level01/IntroExercises.scala
Original file line number Diff line number Diff line change
@@ -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?
Expand All @@ -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
}
}
}

0 comments on commit 69f405d

Please sign in to comment.