Merge pull request #17 from CEPoole/ScalaAdditions

Added to the Scala sheets
pull/18/head
Igor Chubin 6 years ago committed by GitHub
commit 98940ecb63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,9 @@ if (check) happy else sad
if (check) happy // same as
if (check) happy else ()
// multiple conditions
if (check) happy else if(secondCheck) lessHappy else ()
// while loop
while (x < 5) { println(x); x += 1}
@ -51,4 +54,18 @@ for (i <- 1 until 5) {
println(i)
}
// recursion
// can yield a "java.lang.StackOverflowError" with large lists
// this is because each recursive call of the function is waiting for the evaluation of the next
def sum(ints: List[Int]): Int = ints match {
case Nil => 0
case x :: tail => x + sum(tail)
}
// tail recursion
// this type of recursion will not throw StackOverflowError
// this is because each recursive call of the function is fully evaluated
def sum(ints: List[Int], accum: Int): Int = ints match {
case Nil => accum
case x :: tail => sum(tail, accum + x)
}

@ -1,6 +1,10 @@
// tuple literal. (Tuple3)
(1,2,3)
// tuple sugar
(1 -> 2) //same as
(1, 2)
// destructuring bind: tuple unpacking via pattern matching.
var (x,y,z) = (1,2,3)
@ -15,6 +19,10 @@ var xs = List(1,2,3)
// more on it: https://www.slideshare.net/Odersky/fosdem-2009-1013261/27
xs(2)
// map (Immutable)
Map(1 -> 'a', 2 -> 'b', 3 -> 'c') //same as
Map((1, 'a'), (2, 'b'), (3, 'c'))
// cons.
1 :: List(2,3)
@ -25,5 +33,3 @@ xs(2)
// sole member of the Unit type (like C/Java void).
() //(empty parens)

@ -58,6 +58,10 @@ def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd
// need trailing underscore to get the partial, only for the sugar version.
val normer = zscore(7, 0.4) _
// using curried parameters with a partially applied function
// can be called as "add(1)(2)" to return "3"
def add(x: Int) = x + (_: Int)
// generic type.
def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)
@ -68,4 +72,13 @@ def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)
// varargs
def sum(args: Int*) = args.reduceLeft(_+_)
// default parameters
def countTo(i: Int = 5) = 1 to i
countTo(3) = Range(1,2,3)
countTo() = Range(1,2,3,4,5)
// higher order functions
// as long as a function returns the correct type, it can be used as a parameter in another function
def sum(a: Int, b: Int): Int = a + b
def double(x: Int): Int = x * 2
double(sum(1, 1)) // returns 4

@ -0,0 +1,17 @@
// Optional values
val s: Option[Int] = Some(22)
val n: Option[Int] = None
// Removing optional values from a collection
val listOfOptions: List[Option[Int]] = List(Some(1), None, Some(2), None, None, Some(3))
listOfOptions.flatten // returns List(1, 2, 3)
// Get the value out of an option
Some(5).get // returns 5
// Handling None
// BAD
None.get // This will throw java.util.NoSuchElementException
// GOOD
None.getOrElse(1) // This will handle the exception and return 1

@ -27,3 +27,20 @@ Some(3) match {
case _ => println("Not 42")
}
// Creating an alias for a match
// This will maintain the original value passed into the match function, using the '@' symbol, and print "Matched Some(3)"
Some(3) match {
case foundSome @ Some(_) => println("Matched " + foundSome)
case _ => println("Matched nothing")
}
// Case Classes
// This method allows you to match on any combination of properties of a case class
case class Example(a: Int, b: String, c: Boolean)
Example(1, "word", true) match {
case Example(3, _, _) => println("Matches any Example where a = 3")
case Example(_, "foo", _) => println("Matches any Example where b = foo")
case Example(_, _, false) => println("Matches any Example where c = false")
case Example(1, "word", true) => println("Matches our Example")
case Example(_, _, _) => println("Matches any other Example")
}

@ -1,11 +1,21 @@
// variables
// variable
// mutable
var x = 5
// constant
// value
// immutable, evaluated once during compilation
// GOOD
val x = 5
// BAD
x=6
x = 5
// lazy value
// immutable, evaluated once but only when called
lazy val x = 5
// definition
// immutable, evaluated every time it is called
def x = 5
// explicit type
var x: Double = 5
val x: Double = 5

Loading…
Cancel
Save