- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 217字
- 2021-07-23 16:25:17
Implementing another feature
Now, we would like to have a nice representation of the person by stating his/her name and age. The test should look like the following:
"Get a human readable representation of the person" in {
val paul = Person(firstName = "Paul", lastName = "Smith", age = 24)
paul.description should be("Paul Smith is 24 years old")
}
Run the test again. We will get a compilation error:
This is expected as the function doesn't exist on the Person class. To implement it, add the expected implementation by setting the cursor on the description() error in the MainSpec.scala class, hitting Alt + Enter, and selecting the create method description.
IntelliJ generates the method for you and sets the implementation to ???. Replace ??? with the expected code:
def description = s"$firstName $lastName is $age ${if (age <= 1) "year" else "years"} old"
By doing so, we defined a method that does not take any parameter and return a string representing Person. In order to simplify the code, we are using a string interpolation to build the string. To use string interpolation, you just have to prepend an s before the first quote. Inside the quote, you can use the wildcard $ so that we can use an external variable and use the bracket after the dollar sign to enter more code than just a variable name.
Execute the test and the result should be green:
The next step is to write a utility function that, given a list of people, returns only the adults.
For the tests, two cases are defined:
"The Person companion object" should {
val (akira, peter, nick) = (
Person(firstName = "Akira", lastName = "Sakura", age = 12),
Person(firstName = "Peter", lastName = "Müller", age = 34),
Person(firstName = "Nick", lastName = "Tagart", age = 52)
)
"return a list of adult person" in {
val ref = List(akira, peter, nick)
Person.filterAdult(ref) should be(List(peter, nick))
}
"return an empty list if no adult in the list" in {
val ref = List(akira)
Person.filterAdult(ref) should be(List.empty[Person])
}
}
Here, we used a tuple to define three variables. This is a convenient way to define multiple variables. The scope of the variables is bounded by the enclosing curly brackets.
Use IntelliJ to create the filterAdult function by using the Alt+ Enter shortcut. The IDE understands that the function should be in the Person companion object and generates it for you.
We implement this method using the for comprehension Scala feature:
object Person {
def filterAdult(persons: List[Person]) : List[Person] = {
for {
person <- persons
if (person.age >= 18)
} yield (person)
}
}
It is a good practice to define the return type of the method, especially when this method is exposed as a public API.
The for comprehension has been used only for demonstration purposes. We can simplify it using the filter method on List. filter is part of the Scala Collections API and is available for many kinds of collections:
def filterAdult(persons: List[Person]) : List[Person] = {
persons.filter(_.age >= 18)
}
- 物聯網+BIM:構建數字孿生的未來
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 互聯網基礎資源技術與應用發展態勢(2021—2023)
- HTML5 Game development with ImpactJS
- 物聯網之霧:基于霧計算的智能硬件快速反應與安全控制
- TD-LTE無線網絡規劃與設計
- 網絡安全應急響應技術實戰指南
- React Cookbook
- 人人都該都懂的互聯網思維
- 物聯網工程導論(第3版)
- Building RESTful Web Services with .NET Core
- 智慧城市中的物聯網技術
- 想象的互動:網絡人際傳播中的印象形成
- ReasonML Quick Start Guide
- 路由與交換技術