scala 语法速查
摘自官网
| variables | ||
Good
|
Variable. | |
Bad
|
Constant. | |
|
Explicit type. | |
| functions | ||
Good
Bad
|
Define function. Hidden error: without = it’s a procedure returning Unit; causes havoc. Deprecated in Scala 2.13. |
|
Good
Bad
|
Define function. Syntax error: need types for every arg. |
|
|
Type alias. | |
vs.
|
Call-by-value.
Call-by-name (lazy parameters). |
|
|
Anonymous function. | |
vs.
|
Anonymous function: underscore is positionally matched arg. | |
|
Anonymous function: to use an arg twice, have to name it. | |
|
Anonymous function: block style returns last expression. | |
|
Anonymous functions: pipeline style (or parens too). | |
|
Anonymous functions: to pass in multiple blocks, need outer parens. | |
|
Currying, obvious syntax. | |
|
Currying, obvious syntax. | |
|
Currying, sugar syntax. But then: | |
|
Need trailing underscore to get the partial, only for the sugar version. | |
|
Generic type. | |
|
Infix sugar. | |
|
Varargs. | |
| packages | ||
|
Wildcard import. | |
|
Selective import. | |
|
Renaming import. | |
|
Import all from java.util except Date. |
|
At start of file:
Packaging by scope:
Package singleton:
|
Declare a package. | |
| data structures | ||
|
Tuple literal (Tuple3). |
|
|
Destructuring bind: tuple unpacking via pattern matching. | |
Bad
|
Hidden error: each assigned to the entire tuple. | |
|
List (immutable). | |
|
Paren indexing (slides). | |
|
Cons. | |
same as
|
Range sugar. | |
|
Empty parens is singleton value of the Unit type. Equivalent to void in C and Java. |
|
| control constructs | ||
|
Conditional. | |
same as
|
Conditional sugar. | |
|
While loop. | |
|
Do-while loop. | |
|
Break (slides). | |
same as
|
For-comprehension: filter/map. | |
same as
|
For-comprehension: destructuring bind. | |
same as
|
For-comprehension: cross product. | |
|
For-comprehension: imperative-ish.sprintf style. |
|
|
For-comprehension: iterate including the upper bound. | |
|
For-comprehension: iterate omitting the upper bound. | |
| pattern matching | ||
Good
Bad
|
Use case in function args for pattern matching. | |
Bad
|
v42 is interpreted as a name matching any Int value, and “42” is printed. |
|
Good
|
`v42` with backticks is interpreted as the existing val v42, and “Not 42” is printed. |
|
Good
|
UppercaseVal is treated as an existing val, rather than a new pattern variable, because it starts with an uppercase letter. Thus, the value contained within UppercaseVal is checked against 3, and “Not 42” is printed. |
|
| object orientation | ||
|
Constructor params - x is only available in class body. |
|
|
Constructor params - automatic public member defined. | |
|
Constructor is class body. Declare a public member. Declare a gettable but not settable member. Declare a private member. Alternative constructor. |
|
|
Anonymous class. | |
|
Define an abstract class (non-createable). | |
|
Define an inherited class. | |
|
Inheritance and constructor params (wishlist: automatically pass-up params by default). | |
|
Define a singleton (module-like). | |
|
Traits. Interfaces-with-implementation. No constructor params. mixin-able. |
|
|
Multiple traits. | |
|
Must declare method overrides. | |
|
Create object. | |
Bad
Good
|
Type error: abstract type. Instead, convention: callable factory shadowing the type. |
|
|
Class literal. | |
|
Type check (runtime). | |
|
Type cast (runtime). | |
|
Ascription (compile time). | |
| options | ||
|
Construct a non empty optional value. | |
|
The singleton empty optional value. | |
but
|
Null-safe optional value factory. | |
same as
|
Explicit type for empty optional value. Factory for empty optional value. |
|
|
Pipeline style. | |
|
For-comprehension syntax. | |
same as
|
Apply a function on the optional value. | |
same as
|
Same as map but function must return an optional value. | |
same as
|
Extract nested option. | |
same as
|
Apply a procedure on optional value. | |
same as
|
Apply function on optional value, return default if empty. | |
same as
|
Apply partial pattern match on optional value. | |
same as
|
true if not empty. |
|
same as
|
true if empty. |
|
same as
|
true if not empty. |
|
same as
|
0 if empty, otherwise 1. |
|
same as
|
Evaluate and return alternate optional value if empty. | |
same as
|
Evaluate and return default value if empty. | |
same as
|
Return value, throw exception if empty. | |
same as
|
Return value, null if empty. |
|
same as
|
Optional value satisfies predicate. | |
same as
|
Optional value doesn't satisfy predicate. | |
same as
|
Apply predicate on optional value or false if empty. |
|
same as
|
Apply predicate on optional value or true if empty. |
|
same as
|
scala 语法速查的更多相关文章
- Markdown 语法速查表
Markdown 语法速查表 1 标题与文字格式 标题 # 这是 H1 <一级标题> ## 这是 H2 <二级标题> ###### 这是 H6 <六级标题> 文 ...
- Python语法速查: 4. 字符串常用操作
返回目录 (1)字符串常用方法 Python3中,字符串全都用Unicode形式,所以省去了很多以前各种转换与声明的麻烦.字符串属于序列,所有序列可用的方法(比如切片等)都可用于字符串. 注意:字符串 ...
- Python语法速查:目录
1. 数据类型与内置函数 2. 列表.元组.字典.集合操作 3. 字符串格式化 4. 字符串常用操作 5. 运算符.math模块.表达式 6. 循环与迭代 7. 函数基础 8. 类与对象 9. 函数进 ...
- IE各版本CSS Hack(兼容性处理)语法速查表
为了兼容IE各个版本,需要在CSS中添加额外的代码,比如以前常用的_width.之所以工作,是因为浏览器会忽略不能解析的样式规则,因此举个例子来说,把_width写在width下面,对于非IE浏览器会 ...
- perl 语法速查
同时学perl.python和shell脚本会很容易将它们的语法搞混,本文主要是一个个人的总结,方便要用时的查询. perl基本语法.安装.帮助文档 文件头格式: #!/usr/bin/perl us ...
- xpath语法速查
xpath的具体学习可以通过w3c查看(链接:http://www.w3school.com.cn/xpath/index.asp) 这里只是将平时用到的几个表格贴出来,以后查询: 这里的xpath我 ...
- Transact-SQL语法速查手册
第1章 Transact-SQL基础 1.1 标识符 一.常规标识符 1. 命名规则: l 第一个字母必须是Unicode2.0标准定义的字母.下划线.at符号(@)和数字符号(#): l 后续字符可 ...
- Python语法速查: 15. 常用数据结构
返回目录 本篇索引 (1)array (2)bisect (3)deque (4)defaultdict (5)namedtuple (6)heapq (7)itertools (1)array ar ...
- Python语法速查: 13. 操作系统服务
返回目录 本篇索引 (1)sys模块 (2)os模块 (3)与Windows相关模块 (4)subprocess模块 (5)signal模块 (1)sys模块 sys模块用于Python解释器及其环境 ...
随机推荐
- PHP http_response_code 网络函数
定义和用法 http_response_code - 获取/设置响应的 HTTP 状态码 版本支持 PHP4 PHP5 PHP7 不支持 支持 支持 语法 http_response_code ([ ...
- Andorid Studio 新建模拟器无法联网问题
1.查看自己本机的dns cmd -> ipconfing /all 2.修改模拟器的dns 跟PC本机一致. 开启模拟器 -> cmd -> adb root (需要root ...
- Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目
Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目 https://blog.csdn.net/alinyua/article/details/8303 ...
- SQL,case ziduan when ziduan_value then 'result'
case a.sex when 0 then '女' when 1 then '男' else '其他' end as sex 当a表的性别字段的value为0时将查询的value转换成 '女',当字 ...
- Horovod 分布式深度学习框架相关
最近需要 Horovod 相关的知识,在这里记录一下,进行备忘: 分布式训练,分为数据并行和模型并行两种: 模型并行:分布式系统中的不同GPU负责网络模型的不同部分.神经网络模型的不同网络层被分配到不 ...
- python配置yaml
我们在做自动化的过程中无论是接口自动化还是UI自动化都会存在很多数据,我们对于自动化中如何存放这些数据也是很重要一点,如果写在代码里的话,每次更换数据就有点繁琐,我们可以通过一个文件存放这些数据,然后 ...
- Vue全局过滤器的使用 运用在时间过滤 内容添加crud
过滤器的使用 msgFormt是你自己定义的过滤器方法, Vue.filter是你自己定义的全局过滤器.没有s 过滤器要有返回值哈 用retuen Vue.filter("msgFo ...
- Day8 - Python基础8 异常处理、反射、单例模式
本节内容: 1:异常处理 2:反射 3:单例模式 1.异常处理 1.异常简介 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户 ...
- poj 1182 食物链 并查集 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1182 题解 可以考虑使用并查集解决 但是并不是简单的记录是否同一组的这般使用 每个动物都有三个并查集 自己 天敌 捕食 并查集 那么在获得 ...
- Java之匿名对象
匿名对象概念 创建对象时,只有创建对象的语句,却没有把对象地址值赋值给某个变量.虽然是创建对象的简化写法,但是应用场景非常有限.匿名对象 :没有变量名的对象. 格式: new 类名(参数列表): // ...