最近在学习使用Scala语言做项目,感觉这门语言实在是太优美了!作为一个本科数学、研究生机器学习专业的混合人才(哈哈),这门语言真的是满足了普通计算机编程(告诉计算机怎么做)和函数式编程(告诉计算机做什么)的所有幻想。学了半个多月,根本停不下来!先来做个总结:

  1. 语法简洁,往往比Java少至少一半的代码量。比如:

    • 支持自动类型判断,可以省去很多类型标志。 e.g.  val x = 2
    • 用伴生对象来生成类,省去new的麻烦。e.g. val cat = Cat("Hello Ketty")
    • 不用return,直接把一个块(可以使if...else...块,for循环块等)的值返回。例如一行代码定义函数:def add(x: Int, y: Int): Int = x + y
    • 用()来统一函数的参数传递与带参类的构造。对类来说,这种写法其实是语法糖,因为中间有自动的转换机制,使得简洁的代码和底层实现可以分离。
    • 程序易读。对比C/C++、Python、Java,Scala是最符合人类理解的程序语言。
  2. 有几乎完全的函数式风格支持。
    • 函数和值一样,是第一等公民。函数也是值,值也可以作为函数。
    • 支持高阶函数、Curry化、lambda运算等函数运算概念。
    • 函数式风格要求函数尽量无副作用,这样一方面适合做单元测试来验证程序的正确性,另外很适合做并行计算!
  3. 可以满足大多数OOP编程需求。
    • 这里就不展开了。
  4. Scala兼具科学计算语言(matlab、R、Python等)的易读性与静态语言(C/C++、Java等)的高效率。

正是由于以上优点,我觉得Scala很有潜力成为下一门普及的编程语言。

另外,关于Scala学习曲线,我的建议是:

  1. 从网上各种博客大概了解Scala的特性,包括上面提到的几条。
  2. 看书:
    • 《Programming in Scala》和《Scala for the Impatient》交替看。前一本是Scala语言创建者写的,非常通俗易懂,包含了Scala的大多数“是什么”和“为什么”;后一本则是总结性的,把Scala的大多少“是什么”告诉你。
    • 《Scala in Depth》适合中等水平的Scala程序员阅读。(计划下一步看完)
  3. 做项目 / 开发程序。

最后,附上《Scala for the Impatient》(中文版《快学Scala》)的所有key notes 以及我做的部分课后题参考答案地址。如果有需要相关电子书的,可以给我发email。


《Scala for the Impatient》key notes

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
  • Beware of missing = in a function definition
  • Exceptions work just like in Java or C++, but you use a "pattern matching" syntax for catch 
  • Scala has no checked exceptions
 
Chapter 3. Working with Arrays
  • Use an Array if the length is fixed, and an ArrayBuffer if the length can vary
  • 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
 
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
  • To avoid the main method, use an object that extends that App trait
  • 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
 
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.

部分《快学Scala》课后题答案(还在更新中):

https://github.com/fengfu-chris/Scala-for-the-Impatient-Exercises

Scala入门指南与建议的更多相关文章

  1. Microsoft Orleans 之 入门指南

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  2. Vue.js 入门指南之“前传”(含sublime text 3 配置)

    题记:关注Vue.js 很久了,但就是没有动手写过一行代码,今天准备入手,却发现自己比菜鸟还菜,于是四方寻找大牛指点,才终于找到了入门的“入门”,就算是“入门指南”的“前传”吧.此文献给跟我一样“白痴 ...

  3. 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍

    我们在前一篇文章微软新神器-Power BI,一个简单易用,还用得起的BI产品中,我们初步介绍了Power BI的基本知识.由于Power BI是去年开始微软新发布的一个产品,虽然已经可以企业级应用, ...

  4. 一起学微软Power BI系列-官方文档-入门指南(2)获取源数据

    我们在文章: 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍中,我们介绍了官方入门文档的第一章.今天继续给大家介绍官方文档中,如何获取数据源的相关内容.虽然是英文,但 ...

  5. 一起学微软Power BI系列-官方文档-入门指南(3)Power BI建模

    我们前2篇文章:一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍 和一起学微软Power BI系列-官方文档-入门指南(2)获取源数据 中,我们介绍了官方入门文档与获取 ...

  6. 一起学微软Power BI系列-官方文档-入门指南(4)Power BI的可视化

    在前面的系列文章中,我们介绍了官方有关获取数据,以及建模的原始文档和基本介绍.今天继续给大家介绍官方文档中,有关可视化的内容.实际上获获取数据和建模更注重业务关系的处理,而可视化则关注对数据的解读.这 ...

  7. 一起学微软Power BI系列-官方文档-入门指南(5)探索数据奥秘

    我们几篇系列文章中,我们介绍了官方入门文档与获取数据等基本知识.今天继续给大家另外一个重点,探索数据奥秘.有了数据源,有了模型,下一步就是如何解析数据了.解析数据的过程需要很多综合技能,不仅仅是需要掌 ...

  8. 一起学微软Power BI系列-官方文档-入门指南(6)Power BI与Excel

    今天介绍了官方入门文档中有关PowerBI和Excel的知识.前几篇入门文档有点仓促,加上最近时间的研究,会有更多技巧性和入门型的文章或者视频发布,最后2篇入门文档将更加详细一点,因为部分文章进行简单 ...

  9. 一起学微软Power BI系列-官方文档-入门指南(7)发布与共享-终结篇+完整PDF文档

    接触Power BI的时间也只有几个月,虽然花的时间不多,但通过各种渠道了解收集,谈不上精通,但对一些重要概念和细节还是有所了解.在整理官方文档的过程中,也熟悉和了解了很多概念.所以从前到后把微软官方 ...

随机推荐

  1. cc1plus: fatal error: emeralddb-pmdMain.d: No such file or directory

    签名autoscan, aclocal, config啥的都没错,最后make 报下面的错,查了各个文件没发现有啥问题,请哪位帮忙卡看 make[1]: Entering directory `/ro ...

  2. Hortonworks 用于做 Sentimental Analysis的Hiveddl.sql 文件

    The hiveddl.sql script has performed the following steps to refine the data: Converted the raw Twitt ...

  3. Python3.5 入门学习记录——变量类型

    前面介绍了如何下载和安装Python3.5的环境,安装好了后开始进行练习,仍然是最基础的使用Python解释器进行Coding,由于我也是初学Python,也是根据网上其他的教程一点一点练习,然后归纳 ...

  4. C++标准程序读书笔记-第三章错误和异常处理

    1.命名空间(namespace)std C++标准程序库中的所有标示符都被定义于一个名为std的namespace 2.标准异常类别 (1)语言本身或标准程序库所抛出的所有异常,都派生自基类exce ...

  5. iOS Dev (20) 用 AVAudioPlayer 播放一个本地音频文件

    iOS Dev (20) 用 AVAudioPlayer 播放一个本地音频文件 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 步骤 第一步:在 Proj ...

  6. linux操作系统下的码农常用工具

    IDE: Pycharm PHPStorm Zend Studio 文本编辑器: VIM Sublime Text 版本管理: svn RapidSVN git git ui 文件对比: Meld D ...

  7. 山寨QQ音乐的布局(一)

    学了两天IOS趁着还没忘光,巩固一下所学知识想做点东西,由于自己的设计能力有限,所以就山寨一下吧,说到山寨怎么能忘了腾讯呢,今天发现QQ音乐的设计风格是扁平化的,小清新风格,所以就山寨一下它吧.. 由 ...

  8. 阿里云 centos 修改iptables

    一.检查iptables服务状态 首先检查iptables服务的状态 [root@woxplife ~]# service iptables status iptables: Firewall is ...

  9. 在android画面切换时设置跟随变动的小圆圈

    首先还是老规律,上传两张效果图: 第一张图:     第二张图:    前言:我们经常在使用各类安卓APP的时候发现会有如图所示的小圆圈显示当前页所在的,甚至一般来说我们的android桌面上也应该有 ...

  10. 在MFC下实现图像放大镜

    当我们想仔细观察某个细微的东西时,一般都会使用放大镜.而要看清显示在计算机屏幕上的图片或文字时通常也可以借助于Windows操作系统附带的放大程序来实现.但该程序只能以固定的放大倍数去进行观看,有时并 ...