Edited, memorised or added to reading queue

on 24-Aug-2014 (Sun)

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

Flashcard 149630773

Tags
#odersky-programming-in-scala-2ed #scala
Question
How can you make the function literal more concise?
someNumbers.filter(x => x > 0)
Answer
someNumbers.filter(_ > 0)

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







#odersky-programming-in-scala-2ed #scala
Multiple underscores mean multiple parameters, not reuse of a single parameter repeatedly. The first underscore represents the first parameter, the second underscore the second parameter, the third underscore the third parameter, and so on.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




#odersky-programming-in-scala-2ed #scala
you can also replace an entire parameter list with an underscore
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Flashcard 149630836

Tags
#odersky-programming-in-scala-2ed #scala
Question
Although you can’t assign a method or nested function to a variable, or pass it as an argument to another function, you can do these things if you wrap the method or nested function in a function value by [...].
Answer
placing an underscore after its name

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 149630852

Tags
#odersky-programming-in-scala-2ed #scala
Question
Now, although sum _ is indeed a partially applied function, it may not be obvious to you why it is called this. It has this name because you are not applying that function to all of its arguments. In the case of sum _, you are applying it to [...].
Answer
none of its arguments

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







Article 149630870


#odersky-programming-in-scala-2ed #p194 #scala

If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by leaving off the underscore if a function is required at that point in the code, e.g. instead of  someNumbers.foreach(println _) you could just write someNumbers.foreach(println)



Flashcard 149630879

Tags
#odersky-programming-in-scala-2ed #scala
Question
If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by [...] if a function is required at that point in the code, e.g. instead of
someNumbers.foreach(println _)
you could just write
[...]
Answer
leaving off the underscore

you could just write
someNumbers.foreach(println)

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by leaving off the underscore if a function is required at that point in the code, e.g. instead of someNumbers.foreach(println _) you could just write someNumbers.foreach(println) <







Flashcard 149630890

Tags
#odersky-programming-in-scala-2ed #scala
Question
If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by leaving off the underscore if a [...], e.g. instead of
someNumbers.foreach(println _)
you could just write
someNumbers.foreach(println)
Answer
function is required at that point in the code (remember that when anything, not specifically a function is required, like in assignment val b = sum _, leaving off _ won't work).

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
If you are writing a partially applied function expression in which you leave off all parameters, such as println _ or sum _, you can express it more concisely by leaving off the underscore if a function is required at that point in the code, e.g. instead of someNumbers.foreach(println _) you could just write someNumbers.foreach(println)







#odersky-programming-in-scala-2ed #scala
the type of args inside the function, which is declared as type “String*” is actually Array[String]
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Flashcard 149630917

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to call this function?
def echo(args: String*) =
  for (arg <- args) println(arg)
Answer
with any number of comma separated strings, e.g. echo("hello", "world!")

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 149630928

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to apply array of strings to echo?
val arr = Array("What's", "up", "doc?")

def echo(args: String*) =
  for (arg <- args) println(arg)
Answer
add colon and _* symbol to the argument:

echo(arr: _*)

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 149630943

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to call this function with named parameters instead of speed(100, 10)?
def speed(distance: Float, time: Float): Float =
  distance / time
Answer
Use equal sign: speed(distance = 100, time = 10)

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







#odersky-programming-in-scala-2ed #scala
It is also to mix positional and named arguments. In that case, the positional arguments come first.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Flashcard 149630970

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to add Console.out as a default value for out parameter?
def printTime(out: java.io.PrintStream) =
  out.println("time = "+ System.currentTimeMillis())
Answer
def printTime(out: java.io.PrintStream = Console.out) =
  out.println("time = "+ System.currentTimeMillis())

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







#odersky-programming-in-scala-2ed #scala
tail-call optimization is limited to situations in which a method or nested function calls itself directly as its last operation, without going through a function value or some other intermediary.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Flashcard 149630997

Tags
#odersky-programming-in-scala-2ed #scala
Question
What is function value?
Answer
A result of partial application of a function without supplying any arguments, e.g.
val funValue = nestedFun _

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 149631008

Tags
#odersky-programming-in-scala-2ed #scala
Question
Explain the second argument of the filesMatching function
def filesMatching(query: String,
    matcher: (String, String) => Boolean) = {
  for (file <- filesHere; if matcher(file.getName, query))
    yield file
}
Answer
It is a function (specifically function value) that takes 2 Strings as arguments and return Boolean

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 149631019

Tags
#odersky-programming-in-scala-2ed #scala
Question
How can I simplify the syntax of a call to filesMatching inside filesEnding?

def filesEnding(query: String) =
  filesMatching(query, (fileName: String, query: String) => fileName.endsWith(query))

def filesMatching(query: String,
    matcher: (String, String) => Boolean) = {
  for (file <- filesHere; if matcher(file.getName, query))
    yield file
}
Answer
def filesEnding(query: String) =
  filesMatching(query, _.endsWith(_))


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 149631030

Tags
#odersky-programming-in-scala-2ed #scala
Question
How can you simplify this code using closure to avoid passing query as an argument to filesMatching, where filesEnding is the public API client function?

def filesEnding(query: String) =
  filesMatching(query, _.endsWith(_))

def filesMatching(query: String,
    matcher: (String, String) => Boolean) = {
  for (file <- filesHere; if matcher(file.getName, query))
    yield file
}
Answer

def filesEnding(query: String) =
  filesMatching(_.endsWith(query))

def filesMatching(matcher: String => Boolean) =
  for (file <- filesHere; if matcher(file.getName))
    yield 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 149631041

Tags
#odersky-programming-in-scala-2ed #scala
Question
How to rewrite this using curried function (using compact notation)?
scala> def plainOldSum(x: Int, y: Int) = x + y
plainOldSum: (x: Int,y: Int)Int
scala> plainOldSum(1, 2)
res4: Int = 3
Answer
scala> def curriedSum(x: Int)(y: Int) = x + y
curriedSum: (x: Int)(y: Int)Int
scala> curriedSum(1)(2)
res5: Int = 3

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 149631069

Tags
#odersky-programming-in-scala-2ed #scala
Question
In any method invocation in Scala in which [...], you can opt to use curly braces to surround the argument instead of parentheses.
Answer
you’re passing in exactly one argument

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 149631076

Tags
#odersky-programming-in-scala-2ed #scala
Question
What do you think the header of withPrintWriter looks like?
val file = new File("date.txt")
withPrintWriter(file) {
  writer => writer.println(new java.util.Date)
}
Answer
def withPrintWriter(file: File)(op: PrintWriter => Unit) {
  // ...
}

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 149631091

Tags
#odersky-programming-in-scala-2ed #scala
Question
In this code you pass in a zero-arg function to delay evaluation; how would you rewrite it to use by-name parameters?

def myAssert(predicate: () => Boolean) =
  if (!predicate())
    throw new AssertionError
    
myAssert(() => 5 > 3)
Answer
change type () => Boolean to => Boolean:
def myAssert(predicate: => Boolean) =
  if (!predicate())
    throw new AssertionError

myAssert(5 > 3)

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 149631106

Tags
#odersky-programming-in-scala-2ed #scala
Question
Why is it an abstract method, not a variable (field) declaration?

abstract class Element {
def contents: Array[String]
}
Answer
because it is def not var or val

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 149631130

Tags
#odersky-programming-in-scala-2ed #scala
Question
If you leave out an extends clause, the Scala compiler implicitly assumes your class extends from [...], which on the Java platform is the same as class java.lang.Object.
Answer
scala.AnyRef

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







#odersky-programming-in-scala-2ed #scala
In Scala, fields and methods belong to the same namespace. This makes it possible for a field to override a parameterless method.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




#odersky-programming-in-scala-2ed #scala
In Scala it is forbidden to define a field and method with the same name in the same class, whereas it is allowed in Java.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Article 149631169


#odersky-programming-in-scala-2ed #p230 #scala

Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names)



Flashcard 149631178

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are [...], methods, types, and packages. By contrast, Scala’s two namespaces are:
  • values (fields, methods, packages, and singleton objects)
  • types (class and trait names)
Answer
fields

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names) </







Flashcard 149631185

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, [...], types, and packages. By contrast, Scala’s two namespaces are:
  • values (fields, methods, packages, and singleton objects)
  • types (class and trait names)
Answer
methods

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names) </ht







Flashcard 149631192

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, [...], and packages. By contrast, Scala’s two namespaces are:
  • values (fields, methods, packages, and singleton objects)
  • types (class and trait names)
Answer
types

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names)







Flashcard 149631199

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and [...]. By contrast, Scala’s two namespaces are:
  • values (fields, methods, packages, and singleton objects)
  • types (class and trait names)
Answer
packages

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names)







Flashcard 149631215

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are:
  • [...]
  • types (class and trait names)
Answer
values (fields, methods, packages, and singleton objects)

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)types (class and trait names)







Flashcard 149631222

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are:
  • values (fields, methods, packages, and singleton objects)
  • [...]
Answer
types (class and trait names)

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Open it
pan>Scala has just two namespaces for definitions in place of Java’s four. Java’s four namespaces are fields, methods, types, and packages. By contrast, Scala’s two namespaces are: values (fields, methods, packages, and singleton objects)<span>types (class and trait names) <span><body><html>







Flashcard 149631233

Tags
#odersky-programming-in-scala-2ed #scala
Question
If a class has a parameter whose sole purpose is to be copied into a field, you can use a [...] instead
Answer
parametric field

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







#odersky-programming-in-scala-2ed #scala
it is possible to add modifiers such as private, protected, or override to parametric field
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Flashcard 149631352

Tags
#odersky-programming-in-scala-2ed #scala
Question
To invoke a superclass constructor, you simply place
Answer
the argument or arguments you want to pass in parentheses following the name of the superclass.

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







Flashcard 149631372

Tags
#odersky-programming-in-scala-2ed #scala
Question
Scala requires override modifier for [...].
Answer
all members that override a concrete member in a parent class

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 149631383

Tags
#odersky-programming-in-scala-2ed #scala
Question
The override modifier is optional if [...].
Answer
a member implements an abstract member with the same name

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