Scala学习——Scala By Example——to be continued
这一篇是来自官网的Scala By Example
即Tutorial后更详细地对Scala怎么用给了示例
该文一开始给了一个快速排序的示例
版本一:
def sort(xs: Array[Int]) {
def swap(i: Int, j: Int) {
val t = xs(i); xs(i) = xs(j);xs(j) = t
}
def sort1(l: Int, r: Int) {
val pivot = xs((l + r) / 2)
var i = l;
var j = r
while (i <= j) {
while (xs(i) < pivot) i += 1
while (xs(j) > pivot) j -= 1
if (i <= j) {
swap(i, j)
i += 1
j -= 1
}
}
if (l < j) sort1(l, j)
if (j < r) sort1(i, r)
}
sort1(0, xs.length - 1)
}
这里,除了定义方法和变量的def val 还有行尾的";",好像其他和Java也多大区别
再来看一个更能体现Scala特点的编程方法,同样是快速排序
def sort(xs: Array[Int]): Array[Int] = {
if (xs.length <= 1) xs
else {
val pivot = xs(xs.length / 2)
Array.concat(
sort(xs filter (pivot >)),
xs filter (pivot ==),
sort(xs filter (pivot <)))
}
}
这样的编程方式更能体现快速排序的本质:
- 如果数组xs只有一个元素,那么久直接返回xs(这里连return关键字都没有)
- 如果xs有一个以上元素,则取中间值
- 根据大于、等于、小于分块
- 先对前面两个Array排序,再排第三个,然后拼接到前面两个的结果。(这应该是concat()函数的功能)
前者是命令式编程,改变了初始Array;而后者是函数是编程,产生了一个新的Array,只不过这需要占用更多的内存
这里的filter是判定函数
def filter(p: T => Boolean): Array[T]
返回判定为true的结果。像filter这样以别的函数为参数或作为结果返回称为高阶函数
def While (p: => Boolean) (s: => Unit) {
if (p) { s ; While(p)(s) }
}
TODO
1.Programing with Actors and Message
这里用一个电子竞拍服务讲解Actor模型。
2.Expressions and Simple Functions
3.First-class Functions
4.Class and Objects
5.Case Classes and Pattern Matching
6.Generic Types and Methods
7.Lists
8.For-Comprehensions
9.Mutable State
10.Computing with Streams
11.Lazy Values
12.Implicit Parameters and Conversions
13.Hindley/Milner Type Inference
14.Abstractions for Concurrency
Scala学习——Scala By Example——to be continued的更多相关文章
- Scala学习资源
Scala学习资源: Scala官方网站:http://www.scala-lang.org/ Scala github:https://github.com/scala/scala Twitter ...
- Scala学习(一)
最近在学习Scala,总结了一下比较基础的知识. 一.Scala简介 1.Scalable Language,是一门多范式的编程语言,是一种纯面向对象的语言,每个值都是对象. 2.特点:①Scalab ...
- 学习Scala第一篇-从hello World开始
最近开始系统性的学习scala.其实之前使用过scala的,比如我在用Gatling这款性能测试工具的时候就接触到了scala了.Gatling本身就是用Scala写的,而且Gatling的性能测试配 ...
- 怎样学习Scala泛函编程
确切来说应该是我打算怎么去学习Scala泛函编程.在网上找不到系统化完整的Scala泛函编程学习资料,只好把能找到的一些书籍.博客.演讲稿.论坛问答.技术说明等组织一下,希望能达到学习目的.关于Sca ...
- scala学习笔记
一 入门 为了增加编程趣味和技能,学习新语言,体会函数式编程和简易的并发管理模型,了解日渐活跃的Spark,尝试下Scala.Scala = Scalable language,作者是Martin O ...
- 学习Scala: 初学者应该了解的知识
Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...
- 【Todo】【读书笔记】大数据Spark企业级实战版 & Scala学习
下了这本<大数据Spark企业级实战版>, 另外还有一本<Spark大数据处理:技术.应用与性能优化(全)> 先看前一篇. 根据书里的前言里面,对于阅读顺序的建议.先看最后的S ...
- scala学习资料
强烈推荐一个s在线学习scala的网站: http://zh.scala-tour.com/#/overview
- Scala学习笔记1(安装)
到 官网下载scala tar包 http://www.scala-lang.org/download/ mac Finder里双击解压. 改名成scala 进命令行, mv ~/Downloads ...
随机推荐
- 【转】sql server日期比较
1. 当前系统日期.时间select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值例如:向日期加上2天select dateadd(day ...
- sql count执行速度测试
要对数据库里面的数据数量进行统计使用,数据库的大概有2000w多的数据.数据库是mysql5.6 用的是远程连接测试 ELECT COUNT(*) 执行语句: select count( *) fro ...
- [javaSE] 并发编程(线程间通信)
新建一个资源类Resource 定义成员变量String name 定义成员变量int age 新建一个输入类Input,实现Runnable接口 定义一个构造方法Input(),传入参数:Resou ...
- POJ1426(KB1-E 暴力搜索)
Find The Multiple Description Given a positive integer n, write a program to find out a nonzero mu ...
- 零基础学python习题 - Python必须知道的基础语法
1. 以下变量命名不正确的是(D) A. foo = the_value B. foo = l_value C. foo = _value D. foo = value_& 2. 计算2的38 ...
- Code Signal_练习题_shapeArea
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtain ...
- 动态计算area位置
window.onresize = adjuest; function adjuest(){ var picw = $(".imgbox img").width(); var pi ...
- 那些年我们对npm 和 cnpm 的误区
1. npm 和 cnpm 的区别 相信很多人都不太明白 npm 和 cnpm 到底是什么东东, 为啥在国内要用 淘宝镜像使用 cnpm, (1) 两者之间只是 node 中包管理器的不同哟, (2) ...
- PHP通过api上传图片
参考:接口实现图片上传 提交端: $url="localhost:805/rdyc/123.jpg"; $img=file_get_contents($url); $img_api ...
- springboot学习入门之三---启动原理
3启动原理 3.1启动类 @SpringBootApplication public class Application { public static void main(String[] args ...