Edited, memorised or added to reading queue

on 08-Feb-2015 (Sun)

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

Flashcard 149670777

Tags
#odersky-programming-in-scala-1ed #scala
Question
to instantiate a new tuple that holds some objects, just [...].
Answer
place the objects in parentheses, separated by commas

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Next Steps in Scala
les are very useful, for example, if you need to return multiple objects from a method. Whereas in Java you would often create a JavaBean-like class to hold the multiple return values, in Scala you can simply return a tuple. And it is simple: <span>to instantiate a new tuple that holds some objects, just place the objects in parentheses, separated by commas. Once you have a tuple instantiated, you can access its elements individually with a dot, underscore, and the one-based index of the element. An example is shown in Listing 3.4:







#odersky-programming-in-scala-1ed #scala
Both mutable and immutable sets offer a + method, but their behavior differs. Whereas a mutable set will add the element to itself, an immutable set will create and return a new set with the element added.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Next Steps in Scala
ala.collection.immutable.Set, which returns an instance of a default, immutable Set. The Scala compiler infers jetSet's type to be the immutable Set[String]. To add a new element to a set, you call + on the set, passing in the new element. <span>Both mutable and immutable sets offer a + method, but their behavior differs. Whereas a mutable set will add the element to itself, an immutable set will create and return a new set with the element added. In Listing 3.5, you're working with an immutable set, thus the + invocation will yield a brand new set. Although mutable sets offer an actual += method, immutable sets do not. In this ca




Flashcard 149670791

Tags
#odersky-programming-in-scala-1ed #scala
Question
Although mutable sets offer an actual += method, immutable sets do not. In this case, jetSet += "Lear", is essentially [...]
Answer

a shorthand for jetSet = jetSet + "Lear"


statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Next Steps in Scala
r differs. Whereas a mutable set will add the element to itself, an immutable set will create and return a new set with the element added. In Listing 3.5, you're working with an immutable set, thus the + invocation will yield a brand new set. <span>Although mutable sets offer an actual += method, immutable sets do not. In this case, the second line of code, "jetSet += "Lear"", is essentially a shorthand for: jetSet = jetSet + "Lear" Thus, in the second line of Listing 3.5, you reassign the jetSet var with a new set containing "Boeing", "Airbus", and "Lear". Finally, the last line of Lis







Flashcard 149670800

Tags
#odersky-programming-in-scala-1ed #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
Next Steps in Scala
ap is empty because you pass nothing to the factory method (the parentheses in "Map[Int, String]()" are empty).[7] On the next three lines you add key/value pairs to the map using the -> and += methods. As illustrated previously, <span>the Scala compiler transforms a binary operation expression like 1 -> "Go to island." into (1).->("Go to island."). Thus, when you say 1 -> "Go to island.", you are actually calling a method named -> on an integer with the value 1, passing in a string with the value "Go to island." This -> method, which you can invoke on any object in a Scala program, returns a two-element tuple containing the key and value.[8] You then pass this tuple to the += method of the map object to which treasureMap refers. Finally, the last line prints the value that corresponds to the key 2 in the treasureMap. If







#odersky-programming-in-scala-1ed #scala
where you'd say "public" in Java, you simply say nothing in Scala. Public is Scala's default access level.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
the class would fail: val acc = new ChecksumAccumulator acc.sum = 5 // Won't compile, because sum is private Note The way you make members public in Scala is by not explicitly specifying any access modifier. Put another way, <span>where you'd say "public" in Java, you simply say nothing in Scala. Public is Scala's default access level. Now that sum is private, the only code that can access sum is code defined inside the body of the class itself. Thus, ChecksumAccumulator won't be of much use to anyone unless we define




#odersky-programming-in-scala-1ed #scala
method parameters in Scala is that they are val s, not var s.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
e ChecksumAccumulator now has two methods, add and checksum, both of which exhibit the basic form of a function definition, shown in Figure 2.1 here. Any parameters to a method can be used inside the method. One important characteristic of <span>method parameters in Scala is that they are vals, not vars.[1] If you attempt to reassign a parameter inside a method in Scala, therefore, it won't compile: def add(b: Byte): Unit = { b = 1 // This won't compile, because b is a val




Flashcard 149670829

Tags
#odersky-programming-in-scala-1ed #scala
Question
whenever you [...], its result type will definitely be Unit . This is true no matter what the body contains, because the Scala compiler can convert any type to Unit .
Answer
leave off the equals sign before the body of a function

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
Classes and Objects
hecksumAccumulator { private var sum = 0 def add(b: Byte) { sum += b } def checksum(): Int = ~(sum & 0xFF) + 1 } Listing 4.1 - Final version of class ChecksumAccumulator. One puzzler to watch out for is that <span>whenever you leave off the equals sign before the body of a function, its result type will definitely be Unit. This is true no matter what the body contains, because the Scala compiler can convert any type to Unit. For example, if the last result of a method is a String, but the method's result type is declared to be Unit, the String will be converted to Unit and its value lost. Here's an example:







#odersky-programming-in-scala-1ed #scala
a semicolon is required if you write multiple statements on a single line
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
4.2 Semicolon inference [link] In a Scala program, a semicolon at the end of a statement is usually optional. You can type one if you want but you don't have to if the statement appears by itself on a single line. On the other hand, <span>a semicolon is required if you write multiple statements on a single line: val s = "hello"; println(s) If you want to enter a statement that spans multiple lines, most of the time you can simply enter it and Scala will separate the statements i




#odersky-programming-in-scala-1ed #scala
When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,




Flashcard 149670848

Tags
#odersky-programming-in-scala-1ed #scala
Question
When [...], it is called that class's companion object.
Answer
a singleton object shares the same name with a class

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

Parent (intermediate) annotation

Open it
When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton ob

Original toplevel document

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,







Flashcard 149670853

Tags
#odersky-programming-in-scala-1ed #scala
Question
When a singleton object shares the same name with a class, it is called that class's [...].

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

Parent (intermediate) annotation

Open it
When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can a

Original toplevel document

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,







Flashcard 149670873

Tags
#odersky-programming-in-scala-1ed #scala
Question
When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in [where?].
Answer
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

Parent (intermediate) annotation

Open it
When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members.

Original toplevel document

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,







Flashcard 149670882

Tags
#odersky-programming-in-scala-1ed #scala
Question
When a singleton object shares the same name with a class, it is called that class's companion object. The class is called the [...].
Answer
companion class of the singleton object

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

Parent (intermediate) annotation

Open it
tml>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members.<html>

Original toplevel document

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,







Flashcard 149670891

Tags
#odersky-programming-in-scala-1ed #scala
Question
A class and its companion object can access [...].
Answer
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

Parent (intermediate) annotation

Open it
a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access <span>each other's private members.<span><body><html>

Original toplevel document

Classes and Objects
s like a class definition, except instead of the keyword class you use the keyword object. Listing 4.2 shows an example. The singleton object in this figure is named ChecksumAccumulator, the same name as the class in the previous example. <span>When a singleton object shares the same name with a class, it is called that class's companion object. You must define both the class and its companion object in the same source file. The class is called the companion class of the singleton object. A class and its companion object can access each other's private members. The ChecksumAccumulator singleton object has one method, named calculate, which takes a String and calculates a checksum for the characters in the String. It also has one private field,







#odersky-programming-in-scala-1ed #scala
Any standalone object with a main method of the proper signature can be used as the entry point into an application.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
shown in the next section. 4.4 A Scala application [link] To run a Scala program, you must supply the name of a standalone singleton object with a main method that takes one parameter, an Array[String], and has a result type of Unit. <span>Any standalone object with a main method of the proper signature can be used as the entry point into an application. An example is shown in Listing 4.3: // In file Summer.scala import ChecksumAccumulator.calculate object Summer { def main(args: Array[String]) { for (arg




#odersky-programming-in-scala-1ed #scala
Scala implicitly imports members of packages java.lang and scala , as well as the members of a singleton object named Predef , into every Scala source file.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Classes and Objects
previous example. This import statement allows you to use the method's simple name in the rest of the file.[5] The body of the main method simply prints out each argument and the checksum for the argument, separated by a colon. Note <span>Scala implicitly imports members of packages java.lang and scala, as well as the members of a singleton object named Predef, into every Scala source file. Predef, which resides in package scala, contains many useful methods. For example, when you say println in a Scala source file, you're actually invoking println on Predef. (Predef.print




Flashcard 149670920

Tags
#odersky-programming-in-scala-1ed #scala
Question
How can you use this other indexOf form as an operator?
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
Basic Types and Operations
n though this indexOf method takes two arguments, you can use it in operator notation. But whenever you call a method that takes multiple arguments using operator notation, you have to place those arguments in parentheses. For example, here's <span>how you use this other indexOf form as an operator (continuing from the previous example): scala> s indexOf ('o', 5) // Scala invokes s.indexOf('o', 5) res1: Int = 8 Any method can be an operator In Scala operators a







#odersky-programming-in-scala-1ed #scala
Scala will transform the expression -2.0 into the method invocation " (2.0).unary_- ".
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Basic Types and Operations
are -2.0, !found, and ~0xFF. As with the infix operators, these prefix operators are a shorthand way of invoking methods. In this case, however, the name of the method has "unary_" prepended to the operator character. For instance, <span>Scala will transform the expression -2.0 into the method invocation "(2.0).unary_-". You can demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Doub




Flashcard 149670942

Tags
#odersky-programming-in-scala-1ed #scala
Question
Scala will transform the expression -2.0 into the method invocation " [...] ".
Answer
(2.0).unary_-

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

Parent (intermediate) annotation

Open it
Scala will transform the expression -2.0 into the method invocation " (2.0).unary_- ".

Original toplevel document

Basic Types and Operations
are -2.0, !found, and ~0xFF. As with the infix operators, these prefix operators are a shorthand way of invoking methods. In this case, however, the name of the method has "unary_" prepended to the operator character. For instance, <span>Scala will transform the expression -2.0 into the method invocation "(2.0).unary_-". You can demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Doub







#odersky-programming-in-scala-1ed #scala
The only identifiers that can be used as prefix operators are + , - , ! , and ~ .
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Basic Types and Operations
demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Double = -2.0 scala> (2.0).unary_- res3: Double = -2.0 <span>The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. But if you define a me




Flashcard 149670952

Tags
#odersky-programming-in-scala-1ed #scala
Question
The only identifiers that can be used as prefix operators are [...] , - , ! , and ~ .
Answer
+

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

Parent (intermediate) annotation

Open it
The only identifiers that can be used as prefix operators are + , - , ! , and ~ .

Original toplevel document

Basic Types and Operations
demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Double = -2.0 scala> (2.0).unary_- res3: Double = -2.0 <span>The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. But if you define a me







Flashcard 149670957

Tags
#odersky-programming-in-scala-1ed #scala
Question
The only identifiers that can be used as prefix operators are + , [...] , ! , and ~ .
Answer
-

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

Parent (intermediate) annotation

Open it
The only identifiers that can be used as prefix operators are + , - , ! , and ~ .

Original toplevel document

Basic Types and Operations
demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Double = -2.0 scala> (2.0).unary_- res3: Double = -2.0 <span>The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. But if you define a me







Flashcard 149670962

Tags
#odersky-programming-in-scala-1ed #scala
Question
The only identifiers that can be used as prefix operators are + , - , [...] , and ~ .
Answer
!

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

Parent (intermediate) annotation

Open it
The only identifiers that can be used as prefix operators are + , - , ! , and ~ .

Original toplevel document

Basic Types and Operations
demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Double = -2.0 scala> (2.0).unary_- res3: Double = -2.0 <span>The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. But if you define a me







Flashcard 149670967

Tags
#odersky-programming-in-scala-1ed #scala
Question
The only identifiers that can be used as prefix operators are + , - , ! , and [...] .
Answer
~

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

Parent (intermediate) annotation

Open it
The only identifiers that can be used as prefix operators are + , - , ! , and ~ .

Original toplevel document

Basic Types and Operations
demonstrate this to yourself by typing the method call both via operator notation and explicitly: scala> -2.0 // Scala invokes (2.0).unary_- res2: Double = -2.0 scala> (2.0).unary_- res3: Double = -2.0 <span>The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. But if you define a me







Flashcard 149670979

Tags
#odersky-programming-in-scala-1ed #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
Basic Types and Operations
The only identifiers that can be used as prefix operators are +, -, !, and ~. Thus, if you define a method named unary_!, you could invoke that method on a value or variable of the appropriate type using prefix operator notation, such as !p. <span>But if you define a method named unary_*, you wouldn't be able to use prefix operator notation, because * isn't one of the four identifiers that can be used as prefix operators. You could invoke the method normally, as in p.unary_*, but if you attempted to invoke it via *p, Scala will parse it as if you'd written *.p, which is probably not what you had in mind![4] Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses. In Scala, you can leave off empty parentheses on method calls. The conven







#odersky-programming-in-scala-1ed #scala
Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

Basic Types and Operations
identifiers that can be used as prefix operators. You could invoke the method normally, as in p.unary_*, but if you attempted to invoke it via *p, Scala will parse it as if you'd written *.p, which is probably not what you had in mind![4] <span>Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses. In Scala, you can leave off empty parentheses on method calls. The convention is that you include parentheses if the method has side effects, such as println(), but you can leave them of




Flashcard 149670993

Tags
#odersky-programming-in-scala-1ed #scala
Question
Postfix operators are methods that [...].
Answer
take no arguments, when they are invoked 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

Parent (intermediate) annotation

Open it
Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses.

Original toplevel document

Basic Types and Operations
identifiers that can be used as prefix operators. You could invoke the method normally, as in p.unary_*, but if you attempted to invoke it via *p, Scala will parse it as if you'd written *.p, which is probably not what you had in mind![4] <span>Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses. In Scala, you can leave off empty parentheses on method calls. The convention is that you include parentheses if the method has side effects, such as println(), but you can leave them of







Flashcard 149670998

Tags
#odersky-programming-in-scala-1ed #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
Basic Types and Operations
he expression above to be 28, you could write the expression like this: (2 + 2) * 7 Given that Scala doesn't have operators, per se, just a way to use methods in operator notation, you may be wondering how operator precedence works. <span>Scala decides precedence based on the first character of the methods used in operator notation (there's one exception to this rule, which will be discussed below). If the method name starts with a *, for example, it will have a higher precedence than a method that starts with a +. Thus 2 + 2 * 7 will be evaluated as 2 + (2 * 7), and a +++ b *** c (in which a, b, and c are variables, and +++ and *** are methods) will be evaluated a +++ (b *** c), because the *** method has a higher precedence than the +++ method. Table 5.3 here shows the precedence given to the first character of a method in decreasing order of precedence, with characters on the same line having the same precedence. The high