快学scala要点
所属分类 scala
浏览量 721
《Scala for the Impatient》《快学Scala》
Chapter 1. The Basics
Using the Scala interpreter
Defining variables with val and var
Numeric types
Using operators and functions
Navigating Scaladoc
Chapter 2. Control Structures and Functions
An if expression has a value
A block has a value — the value of its last expression
代码块有值 最后一行的值
The Scala for loop is like an "enhanced" Java for loop
Semicolons are (mostly) optional 分号可选
The Void type is Unit
Avoid using return in a function 避免在函数中使用return
Beware of missing = in a function definition 注意函数定义中的 =
Exceptions work just like in Java or C++, but you use a "pattern matching" syntax for catch
异常处理 catch 中使用模式匹配
Scala has no checked exceptions 无 checked 异常
Chapter 3. Working with Arrays
Use an Array if the length is fixed, and an ArrayBuffer if the length can vary
ArrayBuffer 长度可变
Don’t use new when supplying initial values
Use () to access elements 用小括号访问元素
Use for (elem <- arr) to traverse the elements
Use for (elem <- arr if …) … yield … to transform into a new array
Scala and Java arrays are interoperable; with ArrayBuffer, use scala.collection.JavaConversions
Scala和Java数组是可互操作的;对于ArrayBuffer,使用scala.collection.JavaConversions
Chapter 4. Maps and Tuples
Scala has a pleasant syntax for creating, querying, and traversing maps
You need to choose between mutable and immutable maps
By default, you get a hash map, but you can also get a tree map
You can easily convert between Scala and Java maps
Tuples are useful for aggregating values
元组对于聚合值非常有用
Chapter 5. Classes
Fields in classes automatically come with getters and setters
You can replace a field with a custom getter / setter without changing the client of a class — that is the "uniform access principle"
Use the @BeanProperty annotation to generate the JavaBeans getXxx / setXxx methods
Every class has a primary constructor that is "interwoven" with the class definition. Its parameters turn into the fields of the class. The primary constructor excuses all statements in the body of the class
Auxiliary constructors are optional. That are called this.
Chapter 6. Objects
Use Objects for singletons and utility methods
A class can have a companion object with the same name. 与类同名的伴生对象
Objects can extend classes or traits
The apply method of an object is usually used for constructing new instance of the companion class
对象的apply方法通常用于构造同伴类的新实例
To avoid the main method, use an object that extends that App trait
继承 App ,无需 main 函数
You can implement enumerations by extending the Enumeration object
Chapter 7. Packages and imports
Packages nest just like inner classes
Package paths are not absolute
A chain x.y.z in a package clause leaves the intermediate packages x and x.y invisible
Package statements without braces at the top of the file extend to the entire file
A package object can hold functions and variables
Import statements can import packages, classes, and objects
Import statements can be anywhere
Import statements can rename and hide menbers
java.lang, scala, and Predef are always imported
Chapter 8. Inheritance
The extends and final keywords are as in Java.
You must use override when you override a method
Only the primary constructors can all the primary superclass constructor
You can override fields
Chapter 9. Files and Regular Expressions
Source.fromFile(…).getLines.toArray yields all lines of a file
Source.fromFile(…).mkString yields the file contents as a string
To convert a string into a number, use the toInt or toDouble method
Use the Java PrintWriter to write text files
"regex".r is a Regex object
Use """…""" if your regular expression contains backslashes or quotes
If a regex pattern has groups, you can extract their contents using the syntax for (regex(var_1,…,var_n) <- string )
Chapter 10. Traits
A class can implement any number of traits
Traits can require that implementing classes have certain fields, methods, or superclasses
Unlike Java Interface, a Scala trait can provide implementations of methods and fields
When you layer multiple traits, the order matters — the trait whose methods execute first goes to the back
Chapter 11. Operators
Identifiers contain either alphanumeric or operator characters
Unary and binary operators are method calls
Operator precedence depends on the first character, associativity on the last
The apply and update methods are called when evaluating expr(args)
Extractors extract tuples or sequences of values from an input
Chapter 12. Higher-Order Functions
Functions are "first-class citizens" in Scala, just like numbers
You can create anonymous functions, usually to give them to other functions
A function argument specifies behavior that should be executed later
Many collection methods take function parameters, applying a function to the values of the collections
There are syntax shortcuts that allow you to express function parameters in a way that is short and easy to read
You can create functions that operate on the blocks of code and look much like the build-in control statement
Chapter 13. Collections
All collections extend the Iterable trait
The three major categories of collections are sequences, sets, and maps.
Scala has mutable and immutable versions of most collections
A Scala list is either empty, or it has a head and a tail which is again a list
Sets are unordered collections
Use a LinkedHashSet to retain the insertion order or a SortedSet to iterate in sorted order
+ adds an elements to an unordered collection;
+: and :+ prepend or append to a sequence;
++ concatenates two collections;
- and -- remove elements
The Iterable and Seq traits have dozens of useful methods for common operations. Check them out before writing tedious loops.
Mapping, folding, and zipping are useful techniques for applying a function or operation to the elements of a collection
Chapter 14. Pattern Matching and Case Classes
The match expression is a better switch, without fall-through
If no pattern matches, a MatchError is thrown. Use the case _ pattern to avoid that
A pattern can include an arbitrary condition, called a guard
You can match on the type of an expression; prefer this over isInstanceOf / asInstanceOf
You can match patterns of arrays, tuples, and case classes, and bind parts of the pattern to variables
In a for expression, non matches are silently skipped
A case class is a class for which the compiler automatically produce the methods that are needed for pattern matching
The common superclass in a case class hierarchy should be sealed
Use the Option type for values that may or may not be present — it is safer than using null
使用 Option 代替 null
Chapter 15. Annotations
You can annotate classes, methods, fields, local variables, parameters, expressions, type parameters and types.
With expressions and types, the annotation follows the annotated item
Annotation have the form @Annotation, @Annotation(value), or @Annotation(name1= value1, …)
@volatile, @transient, @strictfp, and @native generate the equivalent Java modifiers
Use @throws to generate Java-compatible throws specifications
The @tailrec annotation lets you verify that a recursive function uses tail call optimization
The assert function takes advantage of the @elidable annotation. You can optionally remove assertions from your Scala program
Use the @deprecated annotation to mark deprecated features.
Chapter 16. XML Processiong
XML literals < like > this < / like> are of type NodeSeq
You can embed Scala code inside XML literals
The child property of a Node yield the child nodes
The attributes property of a Node yields a MetaData object containing the node attributes.
The \ and \\ operators carry out XPath-Like matches
You can match mode patterns with XML literals in case clauses
Use the RuleTransformer with RewriteRule instance to transform descendants of a node
The XML object interfaces with Java XML methods for loading and saving
The ConstructingParser is an alternate parser that preserves comments and CDATA sections
Chapter 17. Type parameters
Classes, traits, methods, and functions can have type parameters
Place the type parameters after the name, enclosed in square brackets
Type bounds have the form T <: UpperBound, T >: LowerBound, T <% ViewBound, T : ContextBound
You can restrict a method with a type constraint such as (implicit ev: T <: < UpperBound)
Use +T (covariance) to indicate that a generic type’s subtype relationship is in the same direction as the parameter T, or -T (contravariance) to indicate the reverse direction
Covariance is appropriate for parameters that denote outputs, such as elements in an immutable collection.
Contravariance is appropriate for parameters that denote inputs, such as function arguments.
Chapter 18. Advanced types
Singleton types are useful for method chaining and methods with object parameters
A type projection includes inner class instances for all objects of an outer class.
A type alias gives a short name for a type.
Structural types are equivalent to "duck typing"
Existential types provide the formalism for wildcard parameters of generic types.
Use a self type declaration to indicate that a trait requires another type.
The "cake pattern" uses self types to implement a dependency injection mechanism
An abstract type must be made concrete in a subclass
A higher-kinded type has a type parameter that is itself a parameterized type.
Chapter 19. Parsing
Alternatives, concatenation, options, and repetitions in a grammar turn into |, ~, opt, and rep in Scala combinator parsers.
With RegexParsers, literal strings and regular expressions match tokens.
Use ^^ to process parse results.
Use pattern matching in a function supplied to ^^ to take apart ~ result.
Use ~> and <~ to discard tokens that are no longer needed after matching.
The respell combinator handles the common case of repeated items with a seperator
A token-based parser is useful for paring languages with reserved words and operations. Be prepared to define your own lexer.
Parsers are functions that consume a reader and yield a parse result: success, failure, or error.
The Failure result provides the details for error reporting.
You may want to add failure clauses to your grammar to improve the quality of error message.
Thanks to operator symbols, implicit conversions, and pattern matching, the parser combinator library makes parser writing easy for anyone who understands context-free grammars. Even if you don’t feel the urge to write your own parsers, you may find this an interesting case study for an effective domain-specific language.
Chapter 20. Actors
Extend the Actor class and provide an act method for each other.
To send a message to an actor, use actor ! message.
Message sending is asynchronous: "send and forget."
To receive messages, an actor calls receive or react, usually in a loop.
The argument to receive / react is a block of case clauses (technically, a partial function)
Actors should never share state. Always send data using messages.
Don’t invoke methods on actors. Communicating by sending messages.
Avoid synchronous messaging — that is, unlink sending a message and waiting for a reply.
Actors can share threads by using react instead of receive, provided the control flow of the message handler is simple.
It is OK to let actors crash, provided you have other actors that monitor their demise. Use linking to set up monitoring relationships.
Chapter 21. Implicits
Implicit conversions are used to convert between types.
You must import implicit conversions so that they are in scope as single identifiers.
An implicit parameter list requests objects of a given type. They can be obtained from implicit objects that are defined as single identifiers in scope, or from the companion object of the desired type.
If an implicit parameter is a single-argument function, it is also used as an implicit conversion.
A context bound of a type parameter requires the existence of an implicit object of the given type.
If it is possible to locate an implicit object, this can serve as evidence that a type conversion is valid.
Chapter 22. Delimited Continuations
A continuation lets you go back to a previous point in a program.
You can capture a continuation in a shift block.
A contination function extends until the end of the enclosing reset block.
A continuation is the "reset of the computation" from the expression containing the shift to the end of the enclosing reset, with the shift replaced by a "hole"
When you call a continuation with an argument, the "hole" is set to the argument.
Code containing shift expressions is rewritten in "continuation-passing stype", or CPS, up to the enclosing reset.
A method containing a shift without a reset must be annotated with a CPS annotation.
Continuation can be used to turn a recursive visit of a tree structure into an iteration.
Continuations can undo the "inversion of control" in a web or GUI application.
https://github.com/fengfu-chris/Scala-for-the-Impatient-Exercises
上一篇
下一篇
akka hello http server
scala中的classOf isInstanceOf asInstanceOf
大数据scala基础
akka Actor
Akka 与 微服务
class 和 case class 及 构造器参数