Skip to content

Commit da380cd

Browse files
committed
Created strict equality operator
1 parent 5c7cb70 commit da380cd

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package advancedfp
22

3-
object Chatbot extends App {
4-
val reply: PartialFunction[String, String] = {
3+
class Chatbot {
4+
def reply: PartialFunction[String, String] = {
55
case "Hi" => "Hello"
66
case "Bye" => "Ciao"
77
case _ => "I don't understand you"
88
}
9+
}
10+
object Chatbot extends App {
11+
val bot = new Chatbot
912

1013
scala.io.Source.stdin.getLines()
11-
.map(reply)
14+
.map(bot.reply)
1215
.foreach(println)
1316
}

src/main/scala/implicits/Equal.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ trait Equal[T] {
66

77
object Equal {
88
def apply[T](a:T, b:T)(implicit equal: Equal[T]): Boolean = equal.apply(a, b)
9+
}
10+
11+
object EqualImplicits {
12+
implicit class RichEqual[T](value: T) {
13+
def ===(other: T)(implicit equal: Equal[T]): Boolean = equal.apply(value, other)
14+
def !==(other: T)(implicit equal: Equal[T]): Boolean = !equal.apply(value, other)
15+
}
916
}

src/main/scala/implicits/User.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ case class User(name: String, email: String) {
66
}
77
}
88

9-
object implicits {
9+
object UserImplicits {
1010
implicit object EqualByName extends Equal[User] {
1111
override def apply(a: User, b: User): Boolean = a.name == b.name
1212
}

src/test/scala/advancedfp/ChatbotSpec.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import org.scalatest.matchers.should.Matchers
55

66
class ChatbotSpec extends AnyFunSpec with Matchers {
77

8+
val bot = new Chatbot
89
describe("speak") {
910
it ("should reply 'Hi' when input is 'Hello'") {
10-
val reply = Chatbot.reply("Hi")
11+
val reply = bot.reply("Hi")
1112

1213
reply should be ("Hello")
1314
}
1415
it ("should reply 'Ciao' when input is 'Bye'") {
15-
val reply = Chatbot.reply("Bye")
16+
val reply = bot.reply("Bye")
1617

1718
reply should be ("Ciao")
1819
}
1920
it ("should reply 'I don't understand you' when input is anything else") {
20-
val reply = Chatbot.reply("hahahaha")
21+
val reply = bot.reply("hahahaha")
2122

2223
reply should be ("I don't understand you")
2324
}

src/test/scala/implicits/UserSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package implicits
22

33
import org.scalatest.funspec.AnyFunSpec
44
import org.scalatest.matchers.should.Matchers
5-
import implicits._
5+
import UserImplicits._
66

77
class UserSpec extends AnyFunSpec with Matchers {
88

0 commit comments

Comments
 (0)