Solution: Write core in C++
- C++ alone more complicated than Scala. Combination makes things worse!
Good to experiment - possible to scale
Academic roots
Concepts not difficult but need to be learned
Learning concepts makes you a better programmer
object Example {
abstract class Tree
case class Branch(left: Tree, right: Tree) extends Tree
case class Leaf(x: Int) extends Tree
val tree1 = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Leaf(4)))
def sumLeaves(t: Tree): Int = t match {
case Branch(l, r) => sumLeaves(l) + sumLeaves(r)
case Leaf(x) => x
}
println("sum of leafs=" + sumLeaves(tree1))
}
Slides are loosely based on
by Noel Welsh and Dave Gurnell.
(5 + 3) * 8
List(1,2,3).toString
val
val myCalculation = (5 + 3) * 8
val myExtendedCalculation = myCalculation * 10
val a : Int = (5 + 3) * 8
val b = ((5 + 3 ) * 8).toString
val c : String = (5 + 3 ) * 8
{
val x = 1 + 1
x + 1
}
val result = {
val x = 1 + 1
x + 1
}
println( { val x = 1 + 1
x + 1} )
(x : Int) => x + 3
(x : Int) => {
val y = 1
x + y
}
res5: Int => Int = ammonite.$sess.cmd5$Helper$$Lambda$3763/1257149155@7b903bac
val f = (x : Int) => {
val y = 1
x + y
}
f(3)
A => B
: map a value of type A
to a value of type B
val f : Int => Int = (x : Int) => {
val y = 1
x + y
}
f: Int => Int = ammonite.$sess.cmd6$Helper$$Lambda$3767/474480687@1ceb390c
square
which takes a number x
(of type Double
) and returns $x \cdot x$.sumOfSquares
which takes two arguments x
and y
and returns $x^2 + y^2$.apply twice
, which takes a function f
of type Double => Double
and a number x
of type Double
as parameters and returns f(f(x)
?compose
, which takes two functions $f$ and $g$ as a parameter and returns the composition of f and g, i.e. $f \circ g := f(g(x))$?Similar to functions, but with special syntax
def add(x : Int, y : Int) = x + y
add(5, 3)
val
before argument: variable is publicclass MyClass(field1 : String, val field2 : Int) {
def printOut() : Unit = {
println(field1 + ", " +field2)
}
}
defined class MyClass
val myClass = new MyClass("abc", 5)
myClass.printOut()
abc, 5
myClass: MyClass = ammonite.$sess.cmd14$Helper$MyClass@6b4fac02
case class Point(x : Double, y : Double)
val p = Point(3, 5)
class PositiveNumber(num : Int) {
// some methods
}
object PositiveNumber {
val MaxValue : Int = java.lang.Integer.MAX_VALUE
val MinValue : Int = 0
}
Vector2D
, which represents a vector in two dimensional space.plus
, which adds two vectors and returns a new vector.dot
, which computes the scalar productTypes in Scala can be parametric
case class Pair[A, B](first : A, second : B)
Usage:
val pair1 = Pair(3, 5.0)
val pair2 : Pair[String, Int] = Pair("abc", 5)
Methods can be parametric
def first[A, B](pair : Pair[A, B]) : A = pair.first
val pair = Pair(3, 5.0)
first(pair)
expression match {
case pattern1 => expression1
case pattern2 => expression2
// ...
}
val res = 2 match {
case 1 => "one"
case 2 => "two"
case _ => 5
}
res: Any = "two"
def matchTest2(x: Any): Int = x match {
case 1 => 1
case "two" => 2
case y: Int => y
case _ => 99
}
matchTest2(55)
defined function matchTest2 res20_1: Int = 55
How these concepts fit together