Scala General Flashcards Preview

Scala > Scala General > Flashcards

Flashcards in Scala General Deck (21)
Loading flashcards...
1
Q

Right-Associative Notation

A

Triggered when operators end with a colon and causes the right entity to be invoked first

2
Q

cons operator ::

A
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
3
Q

:::

A

Prepends a list

  val result = List(1,2) ::: List(2,3)
  // List(1,2,2,3)
4
Q

++

A

Appends another collection to a List

List(1,2) ++ Set(2,3,4,4) //List(1,2,3,4)

5
Q

distinct

A

Returns a version of a list without duplicate elements

List(3,2,1,3,2).distinct // List(3,2,1)

6
Q

drop

A

Subtracts the first n elements from a list

List(‘a’,’b’,’c’,’d’) drop 2 // List(‘c’,’d’)

7
Q

flatten

A

Converts a list of lists into a single list of elements

List(List(1,2), List(3,4)).flatten //List(1,2,3,4)

8
Q

partition

A

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 (_

9
Q

take

A

Extracts the first n elements from the list

List(1,2,3,4,5) take 2 // List(1,2)

10
Q

zip

A

Combines two lists into a list of tuples

List(1,2,3) zip List(‘a’,’b’,’c’) // List((1,’a’), (2,’b’), (3,’c’))

11
Q

:+

A

Appends second argument onto a list

List(1,2,3) :+ 6 // List(1,2,3,6)

12
Q

overloaded method

A

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(", "))
  }
13
Q

apply method

A

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)

14
Q

lazy value

A

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)
15
Q

importing using an alias

A

import collection.mutable.{Map=>MutMap}

val m1 = Map(1 -> 2)
val m2 = MutMap(2 -> 4)

16
Q

protected methods

A

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

17
Q

private methods/fields

A

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

18
Q

sealed class

A

Restrict the subclasses of a class to be located in the same file as the parent class

19
Q

final class/method/val

A

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.

20
Q

object

A

A type of class that can have no more than one instance (singleton).

21
Q

companion object

A

An object that shares the same name as a class and is defined together in the same file as the class.