一、基本介绍
Scala 是运行在 Java 虚拟机(Java Virtual Machine)之上,因此具有如下特点:
1. 轻松实现和丰富的 Java 类库互联互通;
2. 它既支持面向对象的编程方式,又支持函数式编程;
3. 它写出的程序像动态语言一样简洁,但事实上它确是严格意义上的静态语言;
4. Scala 就像一位武林中的集大成者,将过去几十年计算机语言发展历史中的精萃集于一身,化繁为简,
为程序员们提供了一种新的选择。设计者马丁·奥得斯基 希望程序员们将编程作为简洁,高效,令人愉快
的工作。同时也让程序员们进行关于编程思想的新的思考。
二、Scala提倡函数式编程(递归思想)
先说下编程范式
1. 在所有的编程范式中,面向对象编程(Object-Oriented Programming)无疑是最大的赢家;
2. 但其实面向对象编程并不是一种严格意义上的编程范式,严格意义上的编程范式分为:命令式编程(Imperative
Programming)、函数式编程(Functional Programming)和逻辑式编程(Logic Programming)。面向对象编程
只是上述几种范式的一个交叉产物,更多的还是继承了命令式编程的基因;
3. 在传统的语言设计中,只有命令式编程得到了强调,那就是程序员要告诉计算机应该怎么做。而递归则通过灵巧
的函数定义,告诉计算机做什么。因此在使用命令式编程思维的程序中,是现在多数程序采用的编程方式,递归出
镜的几率很少,而在函数式编程中,大家可以随处见到递归的方式。
三、Scala中循环不建议使用while和do…while,而建议使用递归。
示例代码:
package com.lj.scala.funcationmost
import java.text.SimpleDateFormat
import java.util.Date
/**
* @author Administrator
* @create 2020-03-19
*/
object Test01 {
def main(args: Array[String]): Unit = {
/*
需求:求 数字相加之和?
*/
// 方式1:传统方式求和
val now01: Date = new Date()
val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date01 = dateFormat.format(now01)
println("方式1开始时间date01:" + date01)
var res01 = BigInt(0)
var num01 = BigInt(1)
val max = BigInt(999999999)
while (num01 < max) {
res01 += num01
num01 += 1
}
println("输出结果res01:" + res01)
val now02: Date = new Date()
val date02 = dateFormat.format(now02)
println("方式1结束时间date02:" + date02)
// 方式2:使用递归的思想求和
val now03: Date = new Date()
val date03 = dateFormat.format(now03)
println("方式2开始时间date03:" + date03)
num01 = 1
def getSum(num: BigInt, sum: BigInt): BigInt = {
if (num < max) {
return getSum(num + 1, sum + num)
} else {
return sum
}
}
val res02 = getSum(1, 0)
println("输出结果res03:" + res02)
val now04: Date = new Date()
val date04 = dateFormat.format(now04)
println("方式2结束时间date04:" + date04)
/*
需求:求一个列表中的最大值
*/
// 使用递归思想
val list01 = List(1, 4, 7, 23, 12, 43, 19, 23, 56, 89, 24, 101, 23, 49, 58, 67, 101)
def getMax(list: List[Int]): Int = {
if (list.isEmpty) {
throw new NoSuchElementException
}
if (list.size == 1) {
list.head
} else if (list.head > getMax(list.tail)) {
list.head
} else {
getMax(list.tail)
}
}
println("list01列表中的最大值max_res01:" + getMax(list01))
/*
需求:翻转字符串
*/
val str01 = "I love you!"
// str01.tail // 截取字符串,除了首字母!
// 使用递归翻转字符串
def reverseStr(str: String): String = {
if (str.length == 1) {
str
} else {
reverseStr(str.tail) + str.head
}
}
println("翻转字符串结果:" + reverseStr(str01))
/*
需求:使用递归求阶乘!
*/
def factorial(num: Int): BigInt = {
if (num == 0) {
1
} else {
num * factorial(num - 1)
}
}
println("num的阶乘结果:" + factorial(32))
}
}
=================================运行结果====================================
方式1开始时间date01:2020-03-19 10:04:53
输出结果res01:499999998500000001
方式1结束时间date02:2020-03-19 10:05:40
方式2开始时间date03:2020-03-19 10:05:40
输出结果res03:499999998500000001
方式2结束时间date04:2020-03-19 10:06:28
list01列表中的最大值max_res01:101
翻转字符串结果:!uoy evol I
num的阶乘结果:263130836933693530167218012160000000
=================================运行结果====================================
对以前的知识回顾,加深基础知识!
学习来自:北京尚硅谷韩顺平老师—尚硅谷大数据技术之Scala
每天进步一点点,也许某一天你也会变得那么渺小!!!