函数式编程是一种"编程范式” (programming paradigm),也就是如何编写程序的方法论。
函数式编程是一种“编程范式”(编程范例),也就是如何编写程序的方法论。
纯粹的函数式编程语言编写的函数没有变量,因此,任意-个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用。而允许使用变量的程序设计语言,由于函数内部的变量状态不确定,同样的输入,可能得到不同的输出,因此,这种函数是有副作用的。
函数式编程的特点:
副作用含义:一个带有副作用的函数不仅有一个返回值,还可能做了修改一个变量,直接修改数据结构,设置-一个对象的成员,打印到终端或者读取用户输入,读取或写入一个文件,在屏幕上绘画,抛出一个异常或以一个错误终止;一个函数在程序执行过程中除了根据输入参数给出运算结果外,没有其他的影响,就称为没有副作用的。
函数式编程优势:
静态编译语言:实现声明变量类型,类型不能改变,编译时检查;
动态编译语言:不用事先声明类型,随时可以赋值为其他类型,编程时不知道什么类型,很难推断;
静态语言和动态语言关键区别是何时获得类型信息,是在编译时间还是运行时间。
强类型语言:不同类型之间操作,必须强制类型转换为同一类型,print(‘a’+1):
弱类型语言:不同类型间可以操作,自动隐式转换。
强类型和弱类型语言的区别关键是类型之间的区别是否严格,例如语言是否会做字符串类型到数字类型的隐式转换。
//弱类型语言vbs
a=1
b=a+"1"+"a";//11a,这里a成了字符串
c=a+1//2,这里a则是int
//强类型语言c++
int a=2;
string b=std::to_string(a),+"1";
int c=a+1;
官网+资料;
《快学scala》;
《Programming in Scala》;
ubuntu20.04
sudo apt install scala
REPL: Read (取值)I-> Evaluation (求值) > Print (打印) > Loop (循环)。
计算表达式:在scala>命令行内, 键入scala代码, 解释器会直接返回结果给你。
wangji@DESKTOP-9TAUMH9:~$ scala
Welcome to Scala 2.11.12 (OpenJDK 64-Bit Server VM, Java 11.0.17).
Type in expressions for evaluation. Or try :help.
scala> 1+1
res0: Int = 2
scala> res0
res1: Int = 2
scala> print(res1)
2
scala> for(i <- 1 to 2) print
<:< RichException char2Character locally refArrayOps
=:= SeqCharSequence charArrayOps long2Long require
??? Set charWrapper longArrayOps seqToCharSequence
ArrayCharSequence Short2short classManifest longWrapper short2Short
ArrowAssoc String classOf manifest shortArrayOps
Boolean2boolean StringCanBuildFrom conforms optManifest shortWrapper
Byte2byte StringFormat double2Double print tuple2ToZippedOps
Character2char Triple doubleArrayOps printf tuple3ToZippedOps
Class any2ArrowAssoc doubleWrapper println unaugmentString
ClassManifest any2Ensuring error readBoolean unitArrayOps
Double2double any2stringadd exceptionWrapper readByte unwrapString
DummyImplicit any2stringfmt fallbackStringCanBuildFrom readChar wrapBooleanArray
Ensuring arrayToCharSequence float2Float readDouble wrapByteArray
Float2float assert floatArrayOps readFloat wrapCharArray
Function assume floatWrapper readInt wrapDoubleArray
Integer2int augmentString genericArrayOps readLine wrapFloatArray
Long2long boolean2Boolean genericWrapArray readLong wrapIntArray
Manifest booleanArrayOps identity readShort wrapLongArray
Map booleanWrapper implicitly readf wrapRefArray
NoManifest byte2Byte int2Integer readf1 wrapShortArray
OptManifest byteArrayOps intArrayOps readf2 wrapString
Pair byteWrapper intWrapper readf3 wrapUnitArray
scala> for(i <- 1 to 2) println(i)
1
2
scala> res1.to
to toByte toDegrees toFloat toInt toOctalString toShort
toBinaryString toChar toDouble toHexString toLong toRadians toString
scala> res1.to
def to(end: Float): Range.Partial[Float,scala.collection.immutable.NumericRange[Float]]
def to(end: Double): Range.Partial[Double,scala.collection.immutable.NumericRange[Double]]
def to(end: Double,step: Double): scala.collection.immutable.NumericRange.Inclusive[Double]
def to(end: Long,step: Long): scala.collection.immutable.NumericRange.Inclusive[Long]
def to(end: Long): scala.collection.immutable.NumericRange.Inclusive[Long]
def to(end: Int,step: Int): scala.collection.immutable.Range.Inclusive
def to(end: Float,step: Float): scala.collection.immutable.NumericRange.Inclusive[Float]
def to(end: Int): scala.collection.immutable.Range.Inclusive
scala> res1.toDouble
res4: Double = 2.0
object helloworld{
def main(args: Array[String]): Unit ={
println("helloworld");
}
}
编译以及运行
mkdir class
scalac -d class helloworld.scala
cd class;scala -classpath . helloworld
配置sbt仓库最新方案见:官网
scala以及sbt的安装,参照我的方法去安装即可
sbt安装方法1:参考:在 Linux 上安装 sbt
ubuntu系统:
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | sudo tee /etc/apt/sources.list.d/sbt_old.list
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add
sudo apt-get update
sudo apt-get install sbt
sbt安装方法2:
ubuntu安装sbt:
echo “deb https://dl.bintray.com/sbt/debian /” | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt
wangji@DESKTOP-9TAUMH9:~/packets$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
Executing: /tmp/apt-key-gpghome.csxhD98teu/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
gpg: key 99E82A75642AC823: "sbt build tool " not changed
gpg: Total number processed: 1
gpg: unchanged: 1
参考下面的链接,可知key:642AC823
apt-key export 642AC823|sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/sbt.gpg
同时修改
wangji@DESKTOP-9TAUMH9:~$ vim /etc/apt/sources.list.d/sbt.list
deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/sbt.gpg] https://dl.bintray.com/sbt/debian /
结果:可以看到增加key成功
wangji@DESKTOP-9TAUMH9:~/packets$ apt-key list
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2015-05-29 [SC]
2EE0 EA64 E40A 89B8 4B2D F734 99E8 2A75 642A C823
uid [ unknown] sbt build tool <scalasbt@gmail.com>
sub rsa4096 2015-05-29 [E]
在~/.sbt下面创建repositories文件,内容如下:
[repositories]
local
osc: https://maven.aliyun.com/nexus/content/groups/public/
typesafe: https://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sonatype-oss-releases
maven-central
sonatype-oss-snapshots
使用sbt
打开终端,进入工程目录,有build.sbt那个
执行sbt -Dsbt.override.build.repos=true(或者直接执行sbt也可以)
执行console
ubuntu install sbt,修复 Key is stored in legacy trusted.gpg keyring
文件名和编码
特殊字符
空格
特殊转义字符
非ASCII字符
scala中简单表达式可以省略括号
//推荐
def square(x: Int) = x*x
val y= if (x < 0) -x else x
if (cond1)
{
//ont line statement
}
else
{
//ont line statement
}
//避免
if (x < 0)
-x
else
x
命名规范
基本原则:驼峰法,命名具有业务含义
val orderName= "name"