Scala Flashcards
Methods and values that aren’t associated with individual instances of a class belong in \_\_\_\_\_\_\_ denoted by using the keyword \_\_\_\_\_ instead of class
singleton objects
object
Most singleton objects do not stand alone, but instead are associated with a class of the same name. When this happens, the singleton object is called the ______ of the class and the class is called the _______ of the object
companion object
companion class
A class and its companion object, if any, must be defined
in the same source file
static is not a keyword in Scala. Instead, all members that would be static, including classes …
should go in a singleton object.
what does => do in scala
creates an instance of a function
in scala every function is
an instance of a class
when you say 1 + 2 in Scala, you are actually
invoking a method called + defined in class int
Traits are like interfaces in Java,
but they can also have
method implementations and even fields
Functional programming is guided by two main ideas.
- A function is a value of the same status as, say, an integer or a string. You can pass functions as arguments to other functions, return them as results from functions
- Operations of a program should map input values to output values rather than change data in place (data structures are immutable)
How would you write this in scala? // this is Java boolean nameHasUpperCase = false; for (int i = 0; i < name.length(); ++i) { if (Character.isUpperCase(name.charAt(i))) { nameHasUpperCase = true; break; } }
val nameHasUpperCase = name.exists(_.isUpper)
The predicate _.isUpper is an example
of a function literal
A function literal can be called a predicate if
its result type is Boolean
What does a static type system do?
classifies variables and expressions according to the
kinds of values they hold and compute
val msg = “Hello, world!”
neither java.lang.String nor String appear anywhere
in the val definition. Why?
this is an example of type inference
the first element in a Scala array
named steps is
steps(0) not steps[0] as in java
what is the syntax for a function literal
a list of named parameters, in parentheses, a right arrow, and then the body of the function.
(x: Int, y: Int) => x + y
when you type 1 + 2
into the Scala interpreter what are you actually doing
invoking a method
named + on the Int object 1, passing in 2 as a parameter
Scala’s List, scala.List, differs from Java’s java.util.List
type in that Scala Lists are …
always immutable
What is the method on Scala’s List that is used for concatenation
:::
What is :: in Scala
“Cons” it prepends a value to the beginning of a list
If a method is used in operator notation, such as a * b, the
method is invoked on the left operand, as in a.*(b)—unless …
the method name ends in a colon. If the method name ends in a colon, the method is
invoked on the right operand. Therefore, in 1 :: twoThree, the :: method
is invoked on twoThree
What is the append operation for a list?
:+
What does this do?
val thrill = “Will” :: “fill” ::
“until” :: Nil
Creates a new List[String] with the
three values “Will”, “fill”, and
“until”
val oneTwoThree = 1 :: 2 :: 3 :: Nil
why is there a Nil at the end?
The reason you need Nil at the end is that :: is defined on class List. If you try to just say 1 :: 2 :: 3, it won’t compile because 3 is an Int, which doesn’t have a :: method