other stuff Flashcards
Curried function form
A ⇒ (B ⇒ C) = (A)(B) ⇒ C
Uncurried function form
(A,B) ⇒ C
First-class functions
treated like any other value
lazy val
computed once, when value is first used, reuses value afterwards
def
computed each time when value is used
val
computed once, when object is created, reuses value afterwards
closure
has free parameters ( which can be modified inside it)
generic classes
take types as a parameter
abstract
needs to be extended
Cannot instantiateabstract classes directly
Abstract methodsmust be implemented by subclasses
Supportspolymorphismand code reuse through inheritance
null
a subtype of any (+user defined) class
final
cannot be overridden or redefined in subclasses
No subclasses possible
super
use super keyword to call the immediate parent class’ method
any
Every class in a Scala execution environment inherits directly or indirectly from this class
Overloading
ability to redefine a function in more than one form, by defining two or more functions in a class sharing the same name
sum(a,b,c) <-> sum(a,b)
LUB
the lowest common ancestor
exists
x.exists(p)
a boolean indicating whether the predicate p holds for some element in x
forall
x.forall(p)
a boolean indicating whether the predicate p holds for all elements of x
count
x.count(p)
the number of elements in x that satisfy the predicate p
takewhile
x.takeWhile(p)
The longest prefix of elements in the collection that all satisfy p
dropwhile
x.dropWhile(p)
The collection without the longest prefix of elements that all satisfy p
x.foreach(f)
executes function f for each element of x
x.map(f)
the collection obtained from applying the function f to every element in x
desugaring
loop ->
yield ->
yield with multiple indices ->
foreach
map, filter
flatmap
x.filter(p)
collection consisting of those elements of x that satisfy the predicate p
x.flatMap(f)
collection obtained from applying the collection-valued function f to every element in x and concatenating the results
xs.foldLeft(z)(op)
Apply binary operation op between successive elements of xs, going left to right and starting with z
xs.foldRight(z)(op)
Apply binary operation op between successive elements of xs, going right to left and starting with z.
monoid
a set and an associated binary oeprator that combines two elements from the set
def[A] |+| (left: A, right: A): A
zipWithIndex
return a set of tuples (element, index)
Nothing
has no values
is a subtype of everyhing
Linear pattern
each variable occurs only once
Sealed classes
No extension of the class is possible outside of the current file
If a function is not sealed, it needs a default case or you will get a warning: match is not exhaustive!
-> allows exhaustive pattern matching
Option
be either Some[T] or None object, which represents a missing value
Tail Recursion
he recursive call is the last thing done by the function
Static typing
check types at compile time, without running the code
Dynamic typing
check types at runtime, while running the code
:<
A :< B
specifies an upper bound
B is upper bound of A
traits
like an abstract class
a class can extend multiple traits (but one class)
a trait also defines a type
Diamond problem
occurs whenever we have some type of multiple inheritance
Linearisation
- Searched in linear order for method implementation:
Deepest first, last trait first
COVARIANCE
If A:B =>
COVARIANCE
→ more specific output
=> then I<a> : I<b></b></a>
(if A is a subtype of B)
CONTRAVARIANCE
If A:B then =>
→ more general input
=> I<b> : I<a></a></b>
INVARIANCE
input and output
variance of:
lists
arrays
seq
covariant
contravariant
covariant
function subtyping
A ⇒ B
functions are contravariant in their arguments but covariant in their results
general ⇒ specific