This cheat sheet is a modified version of the #progfun forum, credits to Laurent Poulain. Most of the things are copied from there. A few things are changed here and there and a few more examples are added to the original version.
Evaluation Rules
- Call by value: evaluates the function arguments before calling the function
- Call by name: evaluates the function first, and then evaluates the arguments if need be
Higher order functions
These are functions that take a function as a parameter or return functions.
Currying
Converting a function with multiple arguments into a function with a single argument that returns another function.
Classes
this references the current object, assert(<condition>) issues AssertionError if condition is not met. See scala.Predef for require, assume and assert.
Operators
myObject myMethod 1 is the same as calling myObject.myMethod(1)
Operator (i.e. function) names can be alphanumeric, symbolic (e.g. x1, *, +?%&, vector_++, counter_=)
The precedence of an operator is determined by its first character, with the following increasing order of priority:
The associativity of an operator is determined by its last character: Right-associative if ending with :, Left-associative otherwise.
Note that assignment operators have lowest precedence. (Read Scala Language Specification 2.9 sections 6.12.3, 6.12.4 for more info).
Class Hierarchies
To create an runnable application in Scala:
Class Organization
- Classes and objects are organized in packages (package myPackage).
- They can be referenced through import statements (import myPackage.MyClass, import myPackage._, import myPackage.{MyClass1, MyClass2}, import myPackage.{MyClass1 => A})
- They can also be directly referenced in the code with the fully qualified name (new myPackage.MyClass1)
- All members of packages scala and java.lang as well as all members of the object scala.Predef are automatically imported.
- Traits are similar to Java interfaces, except they can have non-abstract members:trait Planar { ... } class Square extends Shape with Planar
- General object hierarchy:scala.Any base type of all types. Has methods hashCode and toString that can be overridden- scala.AnyVal base type of all primitive types. (scala.Double, scala.Float, etc.)- scala.AnyRef base type of all reference types. (alias of java.lang.Object, supertype of java.lang.String,scala.List, any user-defined class)- scala.Null is a subtype of any scala.AnyRef (null is the only instance of type Null), and scala.Nothing is a subtype of any other type without any instance.
Type Parameters
Similar to Java generics. These can apply to classes, traits or functions.
Collections
Scala defines several collection classes:
Base Classes
- Iterable (collections you can iterate on)
- Seq (ordered sequences)
- Set
- Map (lookup data structure)
Immutable Collections
- List (linked list, provides fast sequential access)
- Stream (same as List, except that the tail is evaluated only on demand)
- Vector (array-like type, implemented as tree of blocks, provides fast random access)
- Range (ordered sequence of integers with equal spacing)
- String (Java type, implicitly converted to a character sequence, so you can treat every string like a Seq[Char])
- Map (collection that maps keys to values)
- Set (collection without duplicate elements)
Mutable Collections
- Array (Scala arrays are native JVM arrays at runtime, therefore they are very performant)
- Scala also has mutable maps and sets; these should only be used if there are performance issues with immutable types
No comments:
Post a Comment