diff --git a/Aritmethic/.idea/compiler.xml b/Aritmethic/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Aritmethic/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Aritmethic/.idea/copyright/profiles_settings.xml b/Aritmethic/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Aritmethic/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Aritmethic/.idea/misc.xml b/Aritmethic/.idea/misc.xml new file mode 100644 index 0000000..800fcd0 --- /dev/null +++ b/Aritmethic/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Aritmethic/.idea/workspace.xml b/Aritmethic/.idea/workspace.xml new file mode 100644 index 0000000..c88c04a --- /dev/null +++ b/Aritmethic/.idea/workspace.xml @@ -0,0 +1,673 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1477213981305 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + scala-sdk-2.11.8 + + + + + + + + 1.8 + + + + + + + + ElementLibrary + + + + + + + + 1.8 + + + + + + + + scala-sdk-2.11.8 + + + + + + + + \ No newline at end of file diff --git a/Chapter6/.idea/compiler.xml b/Chapter6/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Chapter6/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter6/.idea/copyright/profiles_settings.xml b/Chapter6/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Chapter6/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Chapter6/.idea/misc.xml b/Chapter6/.idea/misc.xml new file mode 100644 index 0000000..eadb753 --- /dev/null +++ b/Chapter6/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter6/.idea/modules.xml b/Chapter6/.idea/modules.xml new file mode 100644 index 0000000..7600eb1 --- /dev/null +++ b/Chapter6/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Chapter6/.idea/scala_compiler.xml b/Chapter6/.idea/scala_compiler.xml new file mode 100644 index 0000000..311a57f --- /dev/null +++ b/Chapter6/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Chapter6/.idea/workspace.xml b/Chapter6/.idea/workspace.xml new file mode 100644 index 0000000..7434bdf --- /dev/null +++ b/Chapter6/.idea/workspace.xml @@ -0,0 +1,517 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1475420792881 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter6/Chapter6.iml b/Chapter6/Chapter6.iml new file mode 100644 index 0000000..513fe2d --- /dev/null +++ b/Chapter6/Chapter6.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter6/src/main/Rational.scala b/Chapter6/src/main/Rational.scala new file mode 100644 index 0000000..15441ce --- /dev/null +++ b/Chapter6/src/main/Rational.scala @@ -0,0 +1,39 @@ +package main + +/** + * Created by claudio on 02/10/16. + */ +class Rational(n: Int, d: Int) { + + require(d!=0) //throw IllegalArgumentException if not + + private val g = gcd(n.abs,d.abs) + + val numer: Int = n / g + val denom: Int = d / g + + def this(n: Int) = this(n, 1) //Auxiliary constructors MUST call another construct + + override def toString = if (d == 1) s"$n" else s"$n/$d" + + def +(that: Rational): Rational = new Rational((numer * that.denom) + + (that.numer * denom), denom * that.denom) + def +(that: Int): Rational = new Rational(numer + that * denom, denom) + def -(that: Rational): Rational = this + new Rational(-that.numer, denom) + def -(that: Int): Rational = this + -that + def *(that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom) + def *(that: Int): Rational = new Rational(numer * that, denom) + def /(that: Rational): Rational = this * that.reciprocal + def /(that: Int): Rational = this * new Rational(1, that) + def reciprocal: Rational = new Rational(denom, numer) + def lessThan(that: Rational): Boolean = numer * that.denom < that.numer * denom + def max(that: Rational): Rational = if (this lessThan that) that else this + + private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) + implicit def IntToRational(x: Int): Rational = new Rational(x) +} + +object Rational{ + def apply(numer: Int): Rational = new Rational(numer) + def apply(numer: Int, denom: Int): Rational = new Rational(numer, denom) +} \ No newline at end of file diff --git a/Chapter6/src/main/main.scala b/Chapter6/src/main/main.scala new file mode 100644 index 0000000..e5c6699 --- /dev/null +++ b/Chapter6/src/main/main.scala @@ -0,0 +1,7 @@ +package main + +object main extends App{ + println("Chapter 6: Functional Objects\n") + val fraction = Rational(1)-Rational(2)+Rational(3,2)/Rational(1,2)+3 + println(s"$fraction") +} \ No newline at end of file diff --git a/Chapter7/.idea/compiler.xml b/Chapter7/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Chapter7/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter7/.idea/copyright/profiles_settings.xml b/Chapter7/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Chapter7/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Chapter7/.idea/misc.xml b/Chapter7/.idea/misc.xml new file mode 100644 index 0000000..757ea42 --- /dev/null +++ b/Chapter7/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter7/.idea/modules.xml b/Chapter7/.idea/modules.xml new file mode 100644 index 0000000..52ccb1e --- /dev/null +++ b/Chapter7/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Chapter7/.idea/workspace.xml b/Chapter7/.idea/workspace.xml new file mode 100644 index 0000000..1671a2c --- /dev/null +++ b/Chapter7/.idea/workspace.xml @@ -0,0 +1,471 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1475765406759 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter7/Chapter7.iml b/Chapter7/Chapter7.iml new file mode 100644 index 0000000..513fe2d --- /dev/null +++ b/Chapter7/Chapter7.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter7/prova/cartesi.scala b/Chapter7/prova/cartesi.scala new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/chiodi.bat b/Chapter7/prova/chiodi.bat new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/chiodi.gif b/Chapter7/prova/chiodi.gif new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/chiodi/altro.com b/Chapter7/prova/chiodi/altro.com new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/ciao.scala b/Chapter7/prova/ciao.scala new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/cose.txt b/Chapter7/prova/cose.txt new file mode 100644 index 0000000..82e5711 --- /dev/null +++ b/Chapter7/prova/cose.txt @@ -0,0 +1 @@ +chiodi cose malusa diff --git a/Chapter7/prova/gradino.scala b/Chapter7/prova/gradino.scala new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/lettoacastello.scala b/Chapter7/prova/lettoacastello.scala new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/malusa.html b/Chapter7/prova/malusa.html new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/nano.doc b/Chapter7/prova/nano.doc new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/prova/scaletta.scala b/Chapter7/prova/scaletta.scala new file mode 100644 index 0000000..e69de29 diff --git a/Chapter7/src/main/main.scala b/Chapter7/src/main/main.scala new file mode 100644 index 0000000..c3846ee --- /dev/null +++ b/Chapter7/src/main/main.scala @@ -0,0 +1,32 @@ +package main +import java.io.File + +/** + * Created by claudio on 06/10/16. + */ +object main extends App{ + println("Chapter 6: Built-in command structures") + println("\nGrepping '.*' from files in 'prova' directory") + val files = new File("prova").listFiles + + def grep(pattern: String) { + def fileLines(file: File) = scala.io.Source.fromFile(file).getLines().toList + for { + file <- files + if file.isFile + line <- fileLines(file) + trimmed = line.trim + if trimmed.matches(pattern) + } println(file+": "+trimmed) + } + grep(".*") + + println("\nListing all .scala files in 'prova' directory") + def scalaFiles = + for{ + file <- files + if file.getName.endsWith(".scala") + } yield file + val stairwayFiles = scalaFiles + stairwayFiles.foreach(f => println(f)) +} diff --git a/First/.idea/compiler.xml b/First/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/First/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/First/.idea/copyright/profiles_settings.xml b/First/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/First/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/First/.idea/misc.xml b/First/.idea/misc.xml new file mode 100644 index 0000000..71d6552 --- /dev/null +++ b/First/.idea/misc.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/First/.idea/modules.xml b/First/.idea/modules.xml new file mode 100644 index 0000000..a3bee3b --- /dev/null +++ b/First/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/First/.idea/modules/first.iml b/First/.idea/modules/first.iml new file mode 100644 index 0000000..0d1ac1d --- /dev/null +++ b/First/.idea/modules/first.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/First/.idea/sbt.xml b/First/.idea/sbt.xml new file mode 100644 index 0000000..9a29dd1 --- /dev/null +++ b/First/.idea/sbt.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/First/.idea/workspace.xml b/First/.idea/workspace.xml new file mode 100644 index 0000000..56970d9 --- /dev/null +++ b/First/.idea/workspace.xml @@ -0,0 +1,524 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1475156302430 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/First/build.sbt b/First/build.sbt new file mode 100644 index 0000000..a792ce8 --- /dev/null +++ b/First/build.sbt @@ -0,0 +1,6 @@ +name := "First" + +version := "1.0" + +scalaVersion := "2.11.8" + \ No newline at end of file diff --git a/First/project/build.properties b/First/project/build.properties new file mode 100644 index 0000000..d638b4f --- /dev/null +++ b/First/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.8 \ No newline at end of file diff --git a/First/project/plugins.sbt b/First/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/First/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/First/src/main/scala/Hello.scala b/First/src/main/scala/Hello.scala new file mode 100644 index 0000000..7d5f687 --- /dev/null +++ b/First/src/main/scala/Hello.scala @@ -0,0 +1,34 @@ +object Hello{ + def main(args: Array[String]) = { + println("Hello, world") + + //the args and predicates + println("Here are the args: ") + args.foreach((arg: String) => { + println(arg) + }) + + //objects + val big = new java.math.BigInteger("12345") + + //arrays and [] (not recomended way to init, too verbose) + val lgrs = new Array[String](3) + lgrs(0) = "Greetings, and" + lgrs(1) = " welcome to" + lgrs(2) = " another LGR thing!" + for (i <- 0 to 2) + print(lgrs(i)) + println() + + // val means only no reassignation: the object CAN change + lgrs.update(2, lgrs(2) + " Let's go thrifting!" + + " [Branches.mp3 plays]") // "obj(args...) = something" == "obj.update(args..., something) + for (i <- 0.to(2)) // operators do not exist: operators are just methods. + print(lgrs.apply(i)) // "obj(args...)" == "obj.apply(args...)" + println() + + // Recommended way to initialize arrays, == "Array.apply(args...)" (factory method) + val techmoans = Array("That's it for the moment, ", "and as always ", "thanks for watching! ", "[muppet clip]") + techmoans.foreach(techmoan => print(techmoan)) + } +} \ No newline at end of file diff --git a/FirstObject/.idea/compiler.xml b/FirstObject/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/FirstObject/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/copyright/profiles_settings.xml b/FirstObject/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/FirstObject/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/FirstObject/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml b/FirstObject/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml new file mode 100644 index 0000000..ee0cbf7 --- /dev/null +++ b/FirstObject/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/misc.xml b/FirstObject/.idea/misc.xml new file mode 100644 index 0000000..bcd7087 --- /dev/null +++ b/FirstObject/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/modules.xml b/FirstObject/.idea/modules.xml new file mode 100644 index 0000000..532d626 --- /dev/null +++ b/FirstObject/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/modules/firstobject-build.iml b/FirstObject/.idea/modules/firstobject-build.iml new file mode 100644 index 0000000..ab207b5 --- /dev/null +++ b/FirstObject/.idea/modules/firstobject-build.iml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/modules/firstobject.iml b/FirstObject/.idea/modules/firstobject.iml new file mode 100644 index 0000000..f3703c3 --- /dev/null +++ b/FirstObject/.idea/modules/firstobject.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/sbt.xml b/FirstObject/.idea/sbt.xml new file mode 100644 index 0000000..65c570f --- /dev/null +++ b/FirstObject/.idea/sbt.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/scala_compiler.xml b/FirstObject/.idea/scala_compiler.xml new file mode 100644 index 0000000..564c87e --- /dev/null +++ b/FirstObject/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/uiDesigner.xml b/FirstObject/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/FirstObject/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/.idea/workspace.xml b/FirstObject/.idea/workspace.xml new file mode 100644 index 0000000..927dcc1 --- /dev/null +++ b/FirstObject/.idea/workspace.xml @@ -0,0 +1,572 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1475419017388 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirstObject/build.sbt b/FirstObject/build.sbt new file mode 100644 index 0000000..8c2da33 --- /dev/null +++ b/FirstObject/build.sbt @@ -0,0 +1,6 @@ +name := "FirstObject" + +version := "1.0" + +scalaVersion := "2.11.8" + \ No newline at end of file diff --git a/FirstObject/project/build.properties b/FirstObject/project/build.properties new file mode 100644 index 0000000..d638b4f --- /dev/null +++ b/FirstObject/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.8 \ No newline at end of file diff --git a/FirstObject/project/plugins.sbt b/FirstObject/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/FirstObject/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/FirstObject/src/main/scala/main/ChecksumAccumulator.scala b/FirstObject/src/main/scala/main/ChecksumAccumulator.scala new file mode 100644 index 0000000..9d9db67 --- /dev/null +++ b/FirstObject/src/main/scala/main/ChecksumAccumulator.scala @@ -0,0 +1,27 @@ +package main + +import scala.collection.mutable + +/** + * Created by claudio on 02/10/16. + */ +class ChecksumAccumulator { + private var sum = 0 + def add(b: Byte): Unit = sum+=b + def checksum(): Int = ~(sum & 0xFF) + 1 +} + +object ChecksumAccumulator { + private val cache = mutable.Map.empty[String, Int] + def calculate(s: String): Int ={ + if(cache.contains(s)){ + cache(s) + } + val ca = new ChecksumAccumulator + for(cs <- s) + ca.add(cs.toByte) + val cs = ca.checksum() + cache += (s -> cs) + cs + } +} \ No newline at end of file diff --git a/FirstObject/src/main/scala/main/FirstObject.scala b/FirstObject/src/main/scala/main/FirstObject.scala new file mode 100644 index 0000000..85c270c --- /dev/null +++ b/FirstObject/src/main/scala/main/FirstObject.scala @@ -0,0 +1,7 @@ +package main + +object FirstObject{ + def main(args: Array[String]) = { + args.foreach(arg => println(arg + ": "+ChecksumAccumulator.calculate(arg))) + } +} \ No newline at end of file diff --git a/Lists/.idea/compiler.xml b/Lists/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Lists/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lists/.idea/copyright/profiles_settings.xml b/Lists/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Lists/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Lists/.idea/misc.xml b/Lists/.idea/misc.xml new file mode 100644 index 0000000..c6d8fb7 --- /dev/null +++ b/Lists/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lists/.idea/modules.xml b/Lists/.idea/modules.xml new file mode 100644 index 0000000..6861d94 --- /dev/null +++ b/Lists/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Lists/.idea/uiDesigner.xml b/Lists/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/Lists/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lists/.idea/workspace.xml b/Lists/.idea/workspace.xml new file mode 100644 index 0000000..98ffefd --- /dev/null +++ b/Lists/.idea/workspace.xml @@ -0,0 +1,529 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1480262859518 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lists/Lists.iml b/Lists/Lists.iml new file mode 100644 index 0000000..4bbbf67 --- /dev/null +++ b/Lists/Lists.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Lists/src/main/Main.scala b/Lists/src/main/Main.scala new file mode 100644 index 0000000..bd44722 --- /dev/null +++ b/Lists/src/main/Main.scala @@ -0,0 +1,43 @@ +package main + +/** + * Created by claudio on 27/11/16. + */ +object Main { + def main(args: Array[String]): Unit = { + val fruits = List("apples", "pears", "bananas") + val fruitsAndLemon = "lemon" :: fruits + val one :: two :: rest = fruitsAndLemon + println(s"$one $two $rest") + + val nums = 3 :: 2 :: 1 :: Nil + println(isort(nums)) + println(msort[Int](_ < _)(nums)) + println(concatenate(1 :: 2 :: 3 :: Nil, 4 :: 5 :: Nil)) + } + def isort(list: List[Int]): List[Int] = list match { + case Nil => Nil + case head :: tail => insert(head, isort(tail)) + } + def insert(head: Int, tail: List[Int]): List[Int] = tail match { + case Nil => head :: Nil + case tailHead :: tailTail => + if(head < tailHead) head :: tail + else tailHead :: insert(head, tailTail) + } + def concatenate[T](first: List[T], second: List[T]): List[T] = first match { + case Nil => second + case firstHead :: firstTail => firstHead :: concatenate(firstTail, second) + } + def msort[T](less: (T,T) => Boolean)(list: List[T]): List[T] = list match{ + case Nil => Nil + case a :: Nil => a :: Nil + case _ => + val length = list.length/2 + val front = msort(less)(list take length) + val ending = msort(less)(list drop length) + if(less(front.head,ending.head)) front ::: ending + else ending ::: front + } +} + diff --git a/VerificaStatica/.idea/compiler.xml b/VerificaStatica/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/VerificaStatica/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/copyright/profiles_settings.xml b/VerificaStatica/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/VerificaStatica/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml b/VerificaStatica/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml new file mode 100644 index 0000000..ee0cbf7 --- /dev/null +++ b/VerificaStatica/.idea/libraries/SBT__org_scala_lang_scala_library_2_11_8_jar.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/misc.xml b/VerificaStatica/.idea/misc.xml new file mode 100644 index 0000000..15d2221 --- /dev/null +++ b/VerificaStatica/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/modules.xml b/VerificaStatica/.idea/modules.xml new file mode 100644 index 0000000..a66de1a --- /dev/null +++ b/VerificaStatica/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/modules/verificastatica-build.iml b/VerificaStatica/.idea/modules/verificastatica-build.iml new file mode 100644 index 0000000..60d73bb --- /dev/null +++ b/VerificaStatica/.idea/modules/verificastatica-build.iml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/modules/verificastatica.iml b/VerificaStatica/.idea/modules/verificastatica.iml new file mode 100644 index 0000000..3623209 --- /dev/null +++ b/VerificaStatica/.idea/modules/verificastatica.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/sbt.xml b/VerificaStatica/.idea/sbt.xml new file mode 100644 index 0000000..65c570f --- /dev/null +++ b/VerificaStatica/.idea/sbt.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/scala_compiler.xml b/VerificaStatica/.idea/scala_compiler.xml new file mode 100644 index 0000000..06f0f53 --- /dev/null +++ b/VerificaStatica/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/VerificaStatica/.idea/workspace.xml b/VerificaStatica/.idea/workspace.xml new file mode 100644 index 0000000..5fc45d5 --- /dev/null +++ b/VerificaStatica/.idea/workspace.xml @@ -0,0 +1,691 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1476047229264 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VerificaStatica/build.sbt b/VerificaStatica/build.sbt new file mode 100644 index 0000000..00a5a40 --- /dev/null +++ b/VerificaStatica/build.sbt @@ -0,0 +1,6 @@ +name := "VerificaStatica" + +version := "1.0" + +scalaVersion := "2.11.8" + \ No newline at end of file diff --git a/VerificaStatica/project/assembly.sbt b/VerificaStatica/project/assembly.sbt new file mode 100644 index 0000000..1741de9 --- /dev/null +++ b/VerificaStatica/project/assembly.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3") \ No newline at end of file diff --git a/VerificaStatica/project/build.properties b/VerificaStatica/project/build.properties new file mode 100644 index 0000000..d638b4f --- /dev/null +++ b/VerificaStatica/project/build.properties @@ -0,0 +1 @@ +sbt.version = 0.13.8 \ No newline at end of file diff --git a/VerificaStatica/project/plugins.sbt b/VerificaStatica/project/plugins.sbt new file mode 100644 index 0000000..14a6ca1 --- /dev/null +++ b/VerificaStatica/project/plugins.sbt @@ -0,0 +1 @@ +logLevel := Level.Warn \ No newline at end of file diff --git a/VerificaStatica/src/main/scala/Main.scala b/VerificaStatica/src/main/scala/Main.scala new file mode 100644 index 0000000..562678f --- /dev/null +++ b/VerificaStatica/src/main/scala/Main.scala @@ -0,0 +1,162 @@ +import scala.io.StdIn + +/** + * Created by claudio on 08/10/16. + */ +object Main { + val vowels = { + val vow = Set('a', 'e', 'i', 'o', 'u') + vow ++ vow.map(_.toUpper) + } + def getReverseString(s: String): String = s.reverse + def getVowels(s: String): String = + for { + c <- s + if vowels.contains(c) + } yield c + def getNumUpperStrings(as: Array[String]): Int = as.count(s => ('A' to 'Z').contains(s(0))) + def getNumSpacesBeforeVowels(s: String): Int ={ + def iterate(i: Int = 0, spaces: Int = 0, sum: Int = 0): Int ={ + if(i >= s.length) sum + else if(s(i) == ' ') iterate(i+1, spaces+1, sum) + else if(vowels.contains(s(i))) iterate(i+1, 0, sum+spaces) + else iterate(i+1, 0, sum) + } + iterate() + } + def getSpacesAsAsterisks(s: String): String = s.map(c => if(c == ' ') '*' else c) + def isMatchBetweenSeq(a: Array[String], b: Array[String]): Array[String] = + for { + a1 <- a + b1 <- b + if a1 == b1 + } yield a1 + def getAlternateOrder(a: Array[Int]): Array[Int] ={ + val limit = a.length.toDouble / 2 - 1 + val max = math.ceil(limit).toInt + val res = for{ + i <- 0 to max + j <- a.indices by a.length-1 + if j == 0 || limit % 1 == 0 || i != max + } yield a(if (j == 0) i else j-i) + res.toArray + } + def isEvenSumEqToOddSum(a: Array[Int]): Boolean = a.count(_ % 2 == 0) == a.count(_ % 2 == 1) + def getReversePositives(a: Array[Int]): Array[Int] = a.filter(_ > 0).reverse + def hasThreeConsecutivesEqual(a: Array[Int]): Boolean = { + for{ + i <- 0 to a.length - 3 + if a(i) == a(i+1) && a(i+1) == a(i+2) + } return true + false + } + def hasDoubled(a: Array[Int]): Boolean = + a.exists(elem => { + for(i <- a; if i == 2*elem) return true + false + }) + def hasPositiveAndThenNegative(a: Array[Int]): Boolean = { + for{ + i <- 0 to a.length - 2 + if a(i) >= 0 && a(i+1) < 0 + } return true + false + } + private def inputString: String ={ + print("Inserisci stringa: ") + StdIn.readLine() + } + private def inputIndex: Int ={ + print("Inserisci la lunghezza della sequenza: ") + val len = StdIn.readInt() + require(len > 0) + len + } + private def inputSeqString: Array[String] ={ + val strings = for(i <- 1 to inputIndex) yield { + print(s"Inserisci #$i stringa: ") + StdIn.readLine() + } + strings.toArray + } + private def inputIntArray: Array[Int] ={ + val ints = for(i <- 1 to inputIndex) yield { + print(s"Inserisci #$i intero: ") + StdIn.readInt() + } + ints.toArray + } + private def printIntArray(a: Array[Int]) = a.foreach(elem => print(elem+" ")) + private def useMenu(menuItems: List[MenuItem]) ={ + println("\nMenù\n") + menuItems.foreach(Console println _) + print("Scelta: ") + val input = StdIn.readInt() + val chosen = menuItems.filter(_.index == input) + if(chosen.isEmpty) println("Voce non riconosciuta") + else chosen.head.action() + } + private def intMenu(array: Array[Int]): Unit ={ + val menuItems = List( + MenuItem(1, "Stampa in modo buffo", () => printIntArray(getAlternateOrder(array))), + MenuItem(2, "Scopri se la somma dei pari equivale alla somme dei dispari", + () => println(if(isEvenSumEqToOddSum(array)) "Somma pari == somma dispari" else "NO")), + MenuItem(3, "Stampa al contrario i positivi", () => printIntArray(getReversePositives(array))), + MenuItem(4, "Scopri se ci sono tre numeri uguali consecutivi", + () => println(if(hasThreeConsecutivesEqual(array)) "Tre consecutivi uguali" else "NO")), + MenuItem(5, "Scopri se un elemento è il doppio di un altro", + () => println(if(hasDoubled(array)) "Elemento = 2*Altro elemento" else "NO")), + MenuItem(6, "Scopri se un positivo e un negativo sono consecutivi", + () => println(if(hasPositiveAndThenNegative(array)) "Positivo consecutivo a negativo" else "NO")), + MenuItem.exit + ) + useMenu(menuItems) + intMenu(array) + } + private def stringMenu(): Unit ={ + val menuItems = List( + MenuItem(1, "Stampa al contrario", () => println("Risultato: "+getReverseString(inputString))), + MenuItem(2, "Stampa solo le vocali", () => println("Risultato: "+getVowels(inputString))), + MenuItem(3, "Trova numero di stringhe", () => println("Risultato: "+getNumUpperStrings(inputSeqString))), + MenuItem(4, "Trova numero spazi prima di una vocale", + () => println("Risultato: "+getNumSpacesBeforeVowels(inputString))), + MenuItem(5, "Sostituisci gli spazi con asterischi", + () => println("Risultato: "+getSpacesAsAsterisks(inputString))), + MenuItem(6, "Trova elementi uguali in due sequenze", () => { + println("Inserisci prima sequenza") + val a = inputSeqString + println("Inserisci seconda sequenza") + val b = inputSeqString + val results = isMatchBetweenSeq(a, b) + if(results.isEmpty) println("Nessuna corrispondenza trovata") + else for(res <- results) println(s"'$res' comune.") + }), + MenuItem.exit + ) + useMenu(menuItems) + stringMenu() + } + def main(argv: Array[String]) = mainMenu + def mainMenu: Unit ={ + val menuItems = List( + MenuItem(1, "Lavora con le stringhe", stringMenu), + MenuItem(2, "Lavora con gli interi", () => { + intMenu(inputIntArray) + }), + MenuItem.exit + ) + useMenu(menuItems) + mainMenu + } +} + +class MenuItem(i: Int, d: String, f: ()=>Unit){ + val index: Int = i + val desc: String = d + val action: () => Unit = f + override implicit def toString: String = s"$index: $desc" +} +object MenuItem{ + def apply(i: Int, d: String, f: ()=>Unit) = new MenuItem(i, d, f) + val exit: MenuItem = MenuItem(0, "Esci dal programma", () => System.exit(0)) +} \ No newline at end of file