Alcuni test su Scala (Tuple, Funzioni di ordine superiore e Pattern Matching)
This commit is contained in:
9
.idea/compiler.xml
generated
9
.idea/compiler.xml
generated
@@ -1,8 +1,17 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="CompilerConfiguration">
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="scala" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
<bytecodeTargetLevel>
|
<bytecodeTargetLevel>
|
||||||
<module name="fpgym" target="8" />
|
<module name="fpgym" target="8" />
|
||||||
|
<module name="scala" target="1.5" />
|
||||||
</bytecodeTargetLevel>
|
</bytecodeTargetLevel>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
6
.idea/scala_compiler.xml
generated
Normal file
6
.idea/scala_compiler.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ScalaCompilerConfiguration">
|
||||||
|
<profile name="Maven 1" modules="scala" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
2
pom.xml
2
pom.xml
@@ -10,7 +10,7 @@
|
|||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
|
<module>scala</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
75
scala/pom.xml
Normal file
75
scala/pom.xml
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.gym.fp</groupId>
|
||||||
|
<artifactId>scala</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<scala.version>2.13.0</scala.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>scala-tools.org</id>
|
||||||
|
<name>Scala-Tools Maven2 Repository</name>
|
||||||
|
<url>http://scala-tools.org/repo-releases</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>scala-tools.org</id>
|
||||||
|
<name>Scala-Tools Maven2 Repository</name>
|
||||||
|
<url>http://scala-tools.org/repo-releases</url>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.scala-lang</groupId>
|
||||||
|
<artifactId>scala-library</artifactId>
|
||||||
|
<version>${scala.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.11</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src/main/scala</sourceDirectory>
|
||||||
|
<testSourceDirectory>src/test/scala</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.scala-tools</groupId>
|
||||||
|
<artifactId>maven-scala-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>testCompile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<scalaVersion>${scala.version}</scalaVersion>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<reporting>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.scala-tools</groupId>
|
||||||
|
<artifactId>maven-scala-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<scalaVersion>${scala.version}</scalaVersion>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</reporting>
|
||||||
|
</project>
|
||||||
139
scala/src/main/scala/org/gym/fp/App.scala
Normal file
139
scala/src/main/scala/org/gym/fp/App.scala
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
package org.gym.fp
|
||||||
|
|
||||||
|
object App {
|
||||||
|
|
||||||
|
def main(args: Array[String]): Unit = {
|
||||||
|
doOperationsOnTuples()
|
||||||
|
doHighOrderFunctions()
|
||||||
|
doPatternMatching()
|
||||||
|
}
|
||||||
|
|
||||||
|
def doOperationsOnTuples(): Unit = {
|
||||||
|
// definire una tupla
|
||||||
|
val tupla = (1, "Hello", true) // può essere di vari tipi
|
||||||
|
println(tupla)
|
||||||
|
|
||||||
|
// Destrutturare una tupla
|
||||||
|
val (number, greeting, isValid) = tupla
|
||||||
|
println(s"$number, $greeting, $isValid")
|
||||||
|
|
||||||
|
// Funzione che prende come argomento una tupla di (Int, String, Boolean)
|
||||||
|
def printTuple(t: (Int, String, Boolean)): Unit = println(s"Da funzione printTuple: $t")
|
||||||
|
|
||||||
|
printTuple(tupla)
|
||||||
|
|
||||||
|
// Pattern matching su lista di tuple
|
||||||
|
val planets = List(
|
||||||
|
("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.2),
|
||||||
|
("Mars", 227.9), ("Jupiter", 778.3)
|
||||||
|
)
|
||||||
|
|
||||||
|
planets.foreach {
|
||||||
|
case ("Earth", distance) =>
|
||||||
|
println(s"Our planet is $distance million kilometers from the sun")
|
||||||
|
case _ =>
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enumerazione
|
||||||
|
object Planet extends Enumeration {
|
||||||
|
type Planet = Value
|
||||||
|
val Mercury, Venus, Earth, Mars, Saturn, Jupiter, Uranus, Neptune = Value
|
||||||
|
}
|
||||||
|
|
||||||
|
import Planet._
|
||||||
|
val planets2 = Set(
|
||||||
|
(Mercury, 57.9), (Venus, 108.2), (Earth, 149.2),
|
||||||
|
(Mars, 227.9), (Jupiter, 778.3)
|
||||||
|
)
|
||||||
|
|
||||||
|
planets2.foreach {
|
||||||
|
case (Jupiter, distance) =>
|
||||||
|
println(s"Our planet is $distance million kilometers from the sun")
|
||||||
|
case _ =>
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def doHighOrderFunctions(): Unit = {
|
||||||
|
|
||||||
|
object SalaryRaiser {
|
||||||
|
private def promotion(salaries: List[Double], promotionsFunction: Double => Double): List[Double] =
|
||||||
|
salaries.map(promotionsFunction)
|
||||||
|
|
||||||
|
def smallPromotion(salaries: List[Double]): List[Double] =
|
||||||
|
promotion(salaries, salary => salary * 1.1)
|
||||||
|
|
||||||
|
def greatPromotion(salaries: List[Double]): List[Double] =
|
||||||
|
promotion(salaries, salary => salary * Math.log(salary))
|
||||||
|
|
||||||
|
def hugePromotion(salaries: List[Double]): List[Double] =
|
||||||
|
promotion(salaries, salary => salary * salary)
|
||||||
|
}
|
||||||
|
|
||||||
|
case class Person(val name: String, val salary: Double)
|
||||||
|
|
||||||
|
val promotions = List(Person("Giacomo", 20000), Person("Mario", 30000)).map(_.salary)
|
||||||
|
SalaryRaiser.smallPromotion(promotions).foreach(println)
|
||||||
|
|
||||||
|
def urlBuilder(ssl: Boolean, domainName: String): (String, String) => String = {
|
||||||
|
val schema = if (ssl) "https://" else "http://"
|
||||||
|
(endpoint: String, query: String) => s"$schema$domainName/$endpoint?$query"
|
||||||
|
}
|
||||||
|
|
||||||
|
val domainName = "www.example.com"
|
||||||
|
def getURL = urlBuilder(ssl = true, domainName)
|
||||||
|
val endpoint = "users"
|
||||||
|
val query = "id=1"
|
||||||
|
val url = getURL(endpoint, query)
|
||||||
|
println(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
def doPatternMatching(): Unit = {
|
||||||
|
// Pattern Matching con le case class
|
||||||
|
abstract class Notification
|
||||||
|
case class Email(sender: String, title: String, body: String) extends Notification
|
||||||
|
case class SMS(caller: String, message: String) extends Notification
|
||||||
|
case class VoiceRecording(contactName: String, link: String) extends Notification
|
||||||
|
|
||||||
|
def showNotification(notification: Notification): String = {
|
||||||
|
notification match {
|
||||||
|
case Email(sender, title, _) =>
|
||||||
|
s"You got an email from $sender with title: $title"
|
||||||
|
case SMS(number, message) =>
|
||||||
|
s"You got an SMS from $number! Message: $message"
|
||||||
|
case VoiceRecording(name, link) =>
|
||||||
|
s"You received a Voice Recording from $name! Click the link to hear it: $link"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val someSMS = SMS("12345", "Are you there?")
|
||||||
|
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
|
||||||
|
|
||||||
|
println(showNotification(someSMS))
|
||||||
|
println(showNotification(someVoiceRecording))
|
||||||
|
|
||||||
|
// Aggiungo un Pattern Guard, cioè una condizione addizionale per cui vale quel matching
|
||||||
|
def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = {
|
||||||
|
notification match {
|
||||||
|
case Email(sender, _, _) if importantPeopleInfo.contains(sender) =>
|
||||||
|
"You got an email from special someone!"
|
||||||
|
case SMS(number, _) if importantPeopleInfo.contains(number) =>
|
||||||
|
"You got an SMS from special someone!"
|
||||||
|
case other =>
|
||||||
|
showNotification(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com")
|
||||||
|
|
||||||
|
val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!")
|
||||||
|
val importantSms = SMS("867-5309", "I'm here! Where are you?")
|
||||||
|
|
||||||
|
println(showImportantNotification(someSMS, importantPeopleInfo))
|
||||||
|
println(showImportantNotification(someVoiceRecording, importantPeopleInfo))
|
||||||
|
println(showImportantNotification(importantEmail, importantPeopleInfo))
|
||||||
|
println(showImportantNotification(importantSms, importantPeopleInfo))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
14
scala/src/test/scala/org/gym/fp/AppTest.scala
Normal file
14
scala/src/test/scala/org/gym/fp/AppTest.scala
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package org.gym.fp
|
||||||
|
|
||||||
|
import org.junit._
|
||||||
|
import Assert._
|
||||||
|
|
||||||
|
@Test
|
||||||
|
class AppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def testOK() = assertTrue(true)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user