Edited, memorised or added to reading list

on 25-Aug-2014 (Mon)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

Flashcard 149629596

Tags
#odersky-programming-in-scala-2ed #scala
Question
What does to stand for in this example?

for (i <- 0 to 2)
  print(greetStrings(i))
Answer
a method call (0).to(2)
if a method takes only one parameter, you can call it without a dot or parentheses.


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629623

Tags
#odersky-programming-in-scala-2ed #scala
Question
What does + really stand for in 2 + 3?
Answer
it is a method call: (2).+(3)
method is named "+"


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629682

Tags
#odersky-programming-in-scala-2ed #scala
Question
What will greetStrings(0) = "Hello" be transformed into?
Answer
greetStrings.update(0, "Hello")


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629693

Tags
#odersky-programming-in-scala-2ed #scala
Question
What method are you calling here and where is it defined?
val numNames = Array("zero", "one", "two")
Answer
a factory method apply which takes variable number of argumetns and is defined on Array companion object
the code gets transformed to
val numNames2 = Array.apply("zero", "one", "two")


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629708

Tags
#odersky-programming-in-scala-2ed #scala
Question
What does the cons expression, e.g. 1::twoThree, gets translated to (in terms of method calls)?
What rule is used?
Answer
twoThree.::(1)
if a method's name used in operator notation ends with colon, it is called on the right operand


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629771

Tags
#odersky-programming-in-scala-2ed #scala
Question
When you add a key-value pair to a map: treasureMap += (2 -> "Dig.") , what is 1 -> "Dig." really?
Answer
It is a method call (1).->("Dig") which returns a tuple containing key and value


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629862

Tags
#odersky-programming-in-scala-2ed #scala
Question
What is the class that has companion object called?
Answer
companion class of that object


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629873

Tags
#odersky-programming-in-scala-2ed #scala
Question
Where a companion class and its companion objects have to be defined?
Answer
in the same source file


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629884

Tags
#odersky-programming-in-scala-2ed #scala
Question
What is special about access control between companion object and companion class?
Answer
they can access each other's private members


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149629968

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to express s.indexOf(’o’, 5) in operator notation?
Answer
s indexOf ('o', 5)


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630011

Tags
#odersky-programming-in-scala-2ed #scala
Question
if you define a method unary_* and then try to call it on object in prefix notation *p, how will Scala parse it and why?
Answer
*.p because * is not one of the four names allowed for prefix methods


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630046

Tags
#odersky-programming-in-scala-2ed #scala
Question
How does scala decide on precedence when methods are used in operator notation, like 2 + 2 * 7 ? What is Scala's default rule here?
Answer
Scala decides precedence based on the first character of the methods used in operator notation (this rule has exceptions). If the method name starts with a *, for example, it will have a higher precedence than a method that starts with a +.


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630206

Tags
#odersky-programming-in-scala-2ed #scala
Question
Why won't this compile?

class Rational(n: Int, d: Int) { // This won’t compile
  require(d != 0)
  override def toString = n +"/"+ d
  def add(that: Rational): Rational =
  new Rational(n * that.d + that.n * d, d * that.d)
}
Answer
Because n and d can only be accessed in methods for this object - they seem to be captured like in closure for toString. They are not fields / members though


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630349

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to call Thread.yield() in Scala, where yield is a reserved word?
Answer
Thread.`yield`()
string enclosed in backticks becomes an identifier, no matter how arbitrary it is.


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630539

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to write Unit as a literal?
Answer
()


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630550

Tags
#odersky-programming-in-scala-2ed #scala
Question
Why doesn't it work?

var line = ""
while ((line = readLine()) != "") // This doesn’t work!
  println("Read: "+ line)

Answer
the result of assignment expression is always Unit, so the above check Unit != "" is always true


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630657

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to change it to exclude upper bound 4?

for (i <- 1 to 4)
  println("Iteration "+ i)

Answer
Use until method instead of to method:

for (i <- 1 until 4)
  println("Iteration "+ i)


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630676

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to move the condition to the generator in the loop?

for (file <- filesHere)
  if (file.getName.endsWith(".scala"))
    println(file)

Answer

for (file <- filesHere if file.getName.endsWith(".scala"))
  println(file)


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630709

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to introduce a val like variable mid-stream into for clauses?
Answer
Simply x=<whatever>, like val x=<whatever> elsewhere in the code, but without val keyword.


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630762

Tags
#odersky-programming-in-scala-2ed #scala
Question
What will calling g() return?
def g(): Int = try { 1 } finally { 2 }
Answer
1


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630784

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to fix this compilation error?
scala> val f = _ + _
<console>:4: error: missing parameter type for expanded
function ((x$1, x$2) => x$1.$plus(x$2))
   val f = _ + _
Answer
Add types, e.g.:

val f = (_: Int) + (_: Int)


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149630859

Tags
#odersky-programming-in-scala-2ed #scala
Question
val b = sum(1, _: Int, 3)
What is b? How is it created in the example above?
Answer
a function (or a "function value") of one argument, integer (function value to be specific). it is created by partial application of sum function


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149631244

Tags
#odersky-programming-in-scala-2ed #scala
Question
How can you simplify this class?

class ArrayElement(conts: Array[String]) extends Element {
  val contents: Array[String] = conts
}
Answer
Create a parametric field:

class ArrayElement(
  val contents: Array[String]
) extends Element


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 149631359

Tags
#odersky-programming-in-scala-2ed #scala
Question
What is this (Array(s)) after superclass name?

class LineElement(s: String) extends ArrayElement(Array(s)) {
  // ...
}
Answer
call to superclass constructor passing this class constructor argument s


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs