Last active
April 3, 2022 20:12
-
-
Save haydenk/c25b9d3ac3e569432291ab565a1cccaa to your computer and use it in GitHub Desktop.
Scala 2 Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter} | |
def withFileWriter(filename: String) (handler: BufferedWriter => Unit): Unit = { | |
val writer = new BufferedWriter(new FileWriter(filename)) | |
try handler(writer) | |
finally writer.close() | |
} | |
def withFileReader(filename: String) (handler: BufferedReader => Unit): Unit = { | |
val reader = new BufferedReader(new FileReader(filename)) | |
try handler(reader) | |
finally reader.close() | |
} | |
withFileWriter("Hello.txt") { | |
writer => { writer.write("Hello\n"); writer.write("World!") } | |
} | |
var result: String = "" | |
withFileReader("Hello.txt") { | |
reader => { result = reader.readLine() + "\n" + reader.readLine() } | |
} | |
assert(result == "Hello\nWorld!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flexibleFizzBuzz(start: Int, end: Int) (callback: String => Unit): Unit = { | |
for (i <- Range(start, end)) { | |
var output: String = "" | |
if (i % 3 == 0) { | |
output += "Fizz" | |
} | |
if (i % 5 == 0) { | |
output += "Buzz" | |
} | |
if (output.isEmpty) { | |
output = s"${i}" | |
} | |
callback(output) | |
} | |
} | |
flexibleFizzBuzz(start = 1, end = 50) { | |
i => println(i) | |
} | |
/* | |
Output: | |
1 | |
2 | |
Fizz | |
4 | |
Buzz | |
Fizz | |
7 | |
8 | |
Fizz | |
Buzz | |
11 | |
Fizz | |
13 | |
14 | |
FizzBuzz | |
16 | |
17 | |
Fizz | |
19 | |
Buzz | |
Fizz | |
22 | |
23 | |
Fizz | |
Buzz | |
26 | |
Fizz | |
28 | |
29 | |
FizzBuzz | |
31 | |
32 | |
Fizz | |
34 | |
Buzz | |
Fizz | |
37 | |
38 | |
Fizz | |
Buzz | |
41 | |
Fizz | |
43 | |
44 | |
FizzBuzz | |
46 | |
47 | |
Fizz | |
49 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Msg(val id: Int, val parent: Option[Int], val text: String) | |
def printMessages(messages: Array[Msg]): Unit = { | |
for (message <- messages) { | |
var indent: Int = 0 | |
message.parent match { | |
case Some(parent) => indent = parent + 2 | |
case None => indent = 0 | |
} | |
println(" " * indent + s"#${message.id} ${message.text}") | |
} | |
} | |
printMessages(Array[Msg]( | |
new Msg(0, None, "Hello"), | |
new Msg(1, Some(0), "World"), | |
new Msg(2, None, "I am Cow"), | |
new Msg(3, Some(2), "Here me moo"), | |
new Msg(4, Some(2), "Here I stand"), | |
new Msg(5, Some(2), "I am Cow"), | |
new Msg(6, Some(5), "Here me moo, moo"), | |
)) | |
/* | |
Output: | |
#0 Hello | |
#1 World | |
#2 I am Cow | |
#3 Here me moo | |
#4 Here I stand | |
#5 I am Cow | |
#6 Here me moo, moo | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment