The de facto languages of Big Data and data science are
Other languages include
In our course, we will be using Scala.
Scala
Scala
Python
def bigger(x, y, f):
return f(x, y)
bigger(1,2, lambda x,y: x > y)
bigger(1,2, lambda x,y: x < y)
# Runtime error
bigger(1,2, lambda x: x)
bigger
is a higher-order function, i.e. a function whose
behaviour is parametrised by another function. f
a function
parameter. To call a HO function, we need to pass a function with the
appropriate argument types. The compiler checks this in the case of
Scala.
Scala
class Foo(val x: Int,
var y: Double = 0.0)
// Type of a is inferred
val a = new Foo(1, 4.0)
println(a.x) //x is read-only
println(a.y) //y is read-write
a.y = 10.0
println(a.y) //y is read-write
a.y = "Foo" // Type mismatch, y is double
val
means a read-only attribute. var
is
read-writeScala
class Foo(val x: Int,
var y: Double = 0.0)
class Bar(x: Int, y: Int, z: Int)
extends Foo(x, y)
trait Printable {
val s: String
def asString() : String
}
class Baz(x: Int, y: Double, private val z: Int)
extends Foo(x, y) with Printable {
override val s: String = s
override def asString(): String = ???
}
Python
class Foo():
def __init__(self, x, y):
self.x = x
self.y = y
class Bar(Foo):
def __init__(self, x, y, z):
Foo.__init__(self, x, y)
self.z = z
In both cases, the traditional rules of method overriding apply. Traits in Scala are similar to default interfaces in Java > 9; in addition, they can include attributes (state).
Scala
Python >= 3.7
Data classes are blueprints for immutable objects. We use them to
represent data records. Both languages implement equals
(or
__eq__
) for them, so we can compare objects directly.
Pattern matching is if..else
on steroids
// Code for demo only, won't compile
value match {
// Match on a value, like if
case 1 => "One"
// Match on the contents of a list
case x :: xs => "The remaining contents are " + xs
// Match on a case class, extract values
case Email(addr, title, _) => s"New email: $title..."
// Match on the type
case xs : List[_] => "This is a list"
// With a pattern guard
case xs : List[Int] if xs.head == 5 => "This is a list of integers"
case _ => "This is the default case"
}
This is by far not an introduction to either programming languages. Please read more here
This work is (c) 2017, 2018, 2019, 2020, 2021 - onwards by TU Delft and Georgios Gousios and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.