Human Factors of XR: Using Human Factors to Design XR Systems
Scala in a nutshell
1. Scala in a nutshell
Kyel John. David
Software Engineer
2. DISCLAIMER
Some of the examples will be very crud. Purpose of the
examples just to get the basic ideas/concepts out
3. What is Scala
Scala is both functional and object oriented language
that operates under the JVM.
Acronym for "Scalable Language"
Supported by TypeSafe Inc
4. Why Scala
Both Functional and Object Oriented
JVM Based language
Higher Order Functions(Functions
as value Types)
"expressive" and "concise"
Almost all Java libraries can be used by Scala
Easier to write multi-threaded applications
Partial Functions
Pattern Matching
And more....
6. Variable Assignments and Declarations
scala> var stockPrice = 20.00
stockPrice: Double = 20.0
scala> var stockPrice = 40.00
stockPrice: Double = 40.0
scala> val numOfShares = 32
numOfShares: Int = 32
scala> numOfShares = 122
<console>:8: error: reassignment to val
numOfShares = 122
^
scala> val distance = { val dx = x x0; val dy = y y0;
sqrt(dx * dx + dy * dy)
}
scala> val person = new Person("Kyel",21)
val - (Read-only)
var - (Read-Write)
7. Variable Assignments and Declarations
//Specifying type information
scala> val person:Person = new Person("Kyel",21)
scala> val listItems: List[Int] = List(1,2,3,4,5)
12. Function Type and Literals
scala> (x: Int, y: Int) => if( x > y ) x else y
(Int, Int) => Int = <function2>
scala> val max = res1
max: (Int, Int) => Int = <function2>
Basic example of a function type
Int => Int
Function Literals - Function with no names(Lambdas in
other language/Anonymous function)
15. Ex of Java to Scala
case class Message(message: String)
def sendPostcards: List[Message] = {
val posters = List("John", "Lloyd", "Cruz"
"Bea Alonzo", "Nina Dobrev",
"Georgina Wilson", "Ashton Kutcher",
"Solenn Heussaff", "Mila Kunis",
"Marian Rivera")
val senders= List("Ding Dong", "Marian")
var messageList: List[Message] = List()
for (h < 0 until posters.length) {
val sender = posters(h)
for (i < 0 until senders.length) {
messageList ::= new Message("Dear " + "Thank you for your wonderful Love, " + sender)
}
}
messageList
}
17. Case Classes
Think of them as value objects or DTOs
All fields are immutable
No more boilerplate code(hashCode and equals has
been generated)
Can be used for Pattern Matching
Maximum of 22 fields only
23. Another Example of Inheritance
scala> class Vehicle(val make: String, var reserve: Boolean){
| def setReserve(r:Boolean):Unit = { reserve = r }
| }
defined class Vehicle
scala> class Sedan(val color: String, reserved: Boolean,make:String)
| extends Vehicle(make, reserved)
defined class Sedan
scala> val vios = new Sedan("Red",false,"Toyota")
vios: Sedan = Sedan@b11975
scala> println(s"Requested a ${vios.color} ${vios.make}")
Requested a Red Toyota
24. Classes with Type Parameters
scala> class Singular[A](element: A) extends Traversable[A] {
| def foreach[B](f: A => B) = f(element)
| }
defined class Singular
scala> val p = new Singular("Planes")
p: Singular[String] = (Planes)
scala> p foreach println
Planes
scala> val name: String = p.head
name: String = Planes
25. Classes with Default Values
scala> class Car(val make: String, var reserved: Boolean = true,
| val year: Int = 2015) {
| override def toString = s"$year $make, reserved = $reserved"
| }
defined class Car
scala> val a = new Car("Toyota")
a: Car = 2015 Toyota, reserved = true
scala> val l = new Car("Lexus", year = 2010)
l: Car = 2010 Lexus, reserved = true
scala> val p = new Car(reserved = false, make = "Volvo")
p: Car = 2015 Volvo, reserved = false
29. Pattern Matching
Case keyword on steroids
Algebraic data types
"pattern matching is a way of assigning names to things
(or binding those names to those things), and possibly
breaking down expressions into subexpressions at the
same time"
Allows you to "Pattern" match
-Strings
-Numbers
-Regular Expressions
-Case classes
31. Collections Framework
Lists, Sets, and Map
Has immutable and mutable impl
Has Map, Reduce, and more implementations
Pattern Matching
Support for HOF
36. Fold vs Fold/Left/Right
Fold operation returns the same type as the elements
on the list
left/right varieties of each operation support unique
return types
Fold has no order of operations
Left only requires fewer traversals compared to right