Scala General Flashcards
Right-Associative Notation
Triggered when operators end with a colon and causes the right entity to be invoked first
cons operator ::
To create a list you can use the following notation: val myList = 1 :: 2 :: 3 :: 4 :: Nil // List(1,2,3,4) val secondList = 42 :: myList secondList.tail == myList // true secondList.head == 42 // true
:::
Prepends a list
val result = List(1,2) ::: List(2,3) // List(1,2,2,3)
++
Appends another collection to a List
List(1,2) ++ Set(2,3,4,4) //List(1,2,3,4)
distinct
Returns a version of a list without duplicate elements
List(3,2,1,3,2).distinct // List(3,2,1)
drop
Subtracts the first n elements from a list
List(‘a’,’b’,’c’,’d’) drop 2 // List(‘c’,’d’)
flatten
Converts a list of lists into a single list of elements
List(List(1,2), List(3,4)).flatten //List(1,2,3,4)
partition
Groups elements into a tuple of two lists based on the result of a true(first list) or false(second list) function
List(1, 2, 3, 4, 5) partition (_
take
Extracts the first n elements from the list
List(1,2,3,4,5) take 2 // List(1,2)
zip
Combines two lists into a list of tuples
List(1,2,3) zip List(‘a’,’b’,’c’) // List((1,’a’), (2,’b’), (3,’c’))
:+
Appends second argument onto a list
List(1,2,3) :+ 6 // List(1,2,3,6)
overloaded method
An implementation where multiple methods are defined with the same name, but take a different number of parameters OR different parameter types.
class Printer(msg: String) { def print(s: String): Unit = println(s"$msg: $s") def print(l: Seq[String]): Unit = print(l.mkString(", ")) }
apply method
Method defined as def apply = … , but the obj.apply(param) does not need to be called, just the object and the parameter obj(param)
class Multiplier(factor: Int) { def apply(input: Int) = input * factor }
val triple = new Multiplier(3)
println(triple(5)) // prints 15 (5*3)
lazy value
Values that are only created the first time they are instantiated.
Class Engine {
val built = {println(“Engine has been built”); “built”}
val ignition = {println(“Engine has been turned on”), “on”}
}
// prints: Engine has been built val littleEngine = new Engine()
// prints: Engine has been turned on // prints: builton println(littleEngine.built + littleEngine.ignition)
importing using an alias
import collection.mutable.{Map=>MutMap}
val m1 = Map(1 -> 2)
val m2 = MutMap(2 -> 4)
protected methods
methods,fields that are protected from being edited by outside classes, however, the method is still accessible by a subclass.
class User { protected val passwrd = “password” }
class ValidUser extends User { def isValid = ! passwrd.isEmpty }
val isValid = new ValidUser().isValid // true
class noAccess { println(new User().passwrd) } // error, no access
private methods/fields
Makes a method/field only accessible within the class where it is defined.
class User(private var password: String) { def update(p: String) { password = p } def validate(p: String) = p == password }
val user = new User("secret") val isValid = user.validate("guess") // false
user.update(“guess”)
val isThisValid = user.validate(“guess”) // true
sealed class
Restrict the subclasses of a class to be located in the same file as the parent class
final class/method/val
Final class members can never be overridden in subclasses. Marking a val, var, or method with final, ensures that the implementation is the one that the subclasses will use.
object
A type of class that can have no more than one instance (singleton).
companion object
An object that shares the same name as a class and is defined together in the same file as the class.