Edited, memorised or added to reading queue
Do you want BuboFlash to help you learning these things?
Click here to log in or create user.
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
#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.
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
#odersky-programming-in-scala-2ed #scala
you can also replace an entire parameter list with an underscore
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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)
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itIf 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)
<
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).
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itIf 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]
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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!")
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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: _*)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
#odersky-programming-in-scala-2ed #scala
It is also to mix positional and named arguments. In that case, the positional arguments come first.
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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())
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
#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.
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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 _
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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(_))
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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) {
// ...
}
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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.
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
#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.
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
#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.
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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)
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itScala 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)
</
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itScala 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
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itScala 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)
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itScala 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)
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itScala 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)
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)
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
Open itpan>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>
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
#odersky-programming-in-scala-2ed #scala
it is possible to add modifiers such as private, protected, or override to parametric field
|
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
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.
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|
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
status | not learned | | measured difficulty | 37% [default] | | last interval [days] | |
---|
repetition number in this series | 0 | | memorised on | | | scheduled repetition | |
---|
scheduled repetition interval | | | last repetition or drill | | | | |
---|