Mauricio Saavedra
A unit of code that returns a value after executed.
"hello world"
1 + 1
5 < 3
val
values cannot change value.
val number: Int = 5
val yes = true
var
values can change value.
var name = "Ramiro"
name = "Bogdan"
Works pretty much as val
.
def number: Int = 15 + 5
def word = "Weird"
def print = println("")
An expression that takes parameters.
(x: Int, y: Int, z:Int) => (x + y)*z
Using def
def product(x: Int, y: Int, z:Int) = (x + y)*z
def sameResultAlways(a: Int, b: Double): Boolean =
{
println("I don't care about your input")
true
}
Constant
true
false
Negation
val a: Boolean
!a
Conjunctions and disjuntions
val a: Boolean
val b: Boolean
a && b
a || b
Comparisons
a < b
a <= b
a > b
a >= b
a == b
a != b
We can build conditional expressions by using a boolean expression and an expression block.
if (a > b) {
println("calculating percentage")
b/a*100
}
else {
println("calculating percentage")
a/b*100
}
Scala is a statically typed with type inference language.
AnyRef types should take parameters.
val middleName: List[Option[String]] =
List(Option("Carlos"), None, Option("Guadalupe"))
No matter the evaluation strategy used it will reduce to the same value, as long as we only have pure functions and the evaluation terminates.
//This function will be evaluated as cbv
def sum(x: Int, y: Int): Int = x + y
The evaluation will be made as follows:
sum(3*2+5, 2+3)
sum(6+5, 2+3)
sum(11, 2+3)
sum(11,5) = 11 + 5 = 16
//both parameter will be evaluated as cbn
def sum(x: => Int, y: => Int): Int = x + y
Will evaluate:
sum(3*2+5, 2+3) = (3*2+5)+(2+3) = (6+5)+(2+3) = 11 + (2+3) = 11 + 5 = 16
The JVM has two main proposes:
Scala is built on top of the JVM. Other languages build the same way are: Kotlin, Groovy, Clojure.
Since Scala is a JVM language it can interoperate with other JVM languages. Some of the most simple examples of Java/Scala interoperability are related to collections. We can pass back and forth between Java and Scala for the following collection types:
Iterator <=> java.util.Iterator
Iterator <=> java.util.Enumeration
Iterable <=> java.lang.Iterable
Iterable <=> java.util.Collection
mutable.Buffer <=> java.util.List
mutable.Set <=> java.util.Set
mutable.Map <=> java.util.Map
mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
The usage of SBT as build tool, simplifies the integration of libraries. Dependencies will be handled by adding them to the buil.sbt file.
For example if you require ScalaTest library, you only have to add:
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.5"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
resolvers += "Artima Maven Repository" at "http://repo.artima.com/releases"
addSbtPlugin("com.artima.supersafe" % "sbtplugin" % "1.1.3")
Once installed we should only import it in the desired project as:
import org.scalatest._
Scaladoc is based on Javadoc, it works by using comments directly before the items that are going to be documented.
It can automatically generate API documentation and will place it under the target directory of your SBT project by just running:
bash $ sbt doc
The result is such:
The documentation is sorted by packages. Specific classes can be sorted by name by using the search bar. Once you are browsing a package you will the screen on the next slide.
On the center there are a list of methods. On the right a list with a (t), (o) or (c) symbol that will take you to the trait, object or class.
Scaladoc comments should start with /** and end with */. In general a good Scaladoc should contain a quick summary of the function, at the beginning. A more complex description of the subject documented can go next. Finally a group of tags, should close the documentation of the subject, which can be: @constructor
, @return
, @throws
, @param
, @tparam
, @see
, @note
, @example
, @usecase
.
For example:
/** This function multiplies the addition of two numbers
*
* @param x term1
* @param y term2
* @param z multiplier
* @return (x + y)*z
*/
def product(x: Int, y: Int, z:Int) = (x + y)*z