Skip to content

Commit ca13cde

Browse files
committed
New chatbot using partial functions
1 parent 2c33d9c commit ca13cde

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package advancedfp
2+
3+
object Chatbot extends App {
4+
val reply: PartialFunction[String, String] = {
5+
case "Hi" => "Hello"
6+
case "Bye" => "Ciao"
7+
case _ => "I don't understand you"
8+
}
9+
10+
scala.io.Source.stdin.getLines()
11+
.map(reply)
12+
.foreach(println)
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package advancedfp
2+
3+
import org.scalatest.funspec.AnyFunSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
class ChatbotSpec extends AnyFunSpec with Matchers {
7+
8+
describe("speak") {
9+
it ("should reply 'Hi' when input is 'Hello'") {
10+
val reply = Chatbot.reply("Hi")
11+
12+
reply should be ("Hello")
13+
}
14+
it ("should reply 'Ciao' when input is 'Bye'") {
15+
val reply = Chatbot.reply("Bye")
16+
17+
reply should be ("Ciao")
18+
}
19+
it ("should reply 'I don't understand you' when input is anything else") {
20+
val reply = Chatbot.reply("hahahaha")
21+
22+
reply should be ("I don't understand you")
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)