Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvenz committed Oct 10, 2023
1 parent 69f405d commit d58f557
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main/scala/introcourse/level01/IntroExercises.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -10,21 +11,24 @@ 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 = 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 = 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
*/
Expand All @@ -33,23 +37,27 @@ object IntroExercises {
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 = a

/** How about this one?
/**
* How about this one?
*/
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
Expand All @@ -61,20 +69,23 @@ object IntroExercises {
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 = 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) = (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.
Expand All @@ -86,7 +97,8 @@ object IntroExercises {
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 = {
pair match {
Expand Down

0 comments on commit d58f557

Please sign in to comment.