From 042bef2dcb399f2708e8569c2843a74d59f42f9c Mon Sep 17 00:00:00 2001 From: rhyme Date: Mon, 16 Jul 2018 17:20:04 +0800 Subject: [PATCH] sprint1.md --- .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/hydra.xml | 9 ++ ..._lang_modules_scala_xml_2_12_1_0_6_jar.xml | 11 ++ ...rg_scala_lang_scala_library_2_12_6_jar.xml | 20 +++ ...rg_scala_lang_scala_reflect_2_12_6_jar.xml | 11 ++ ...org_scalactic_scalactic_2_12_3_0_5_jar.xml | 11 ++ ...org_scalatest_scalatest_2_12_3_0_5_jar.xml | 11 ++ .idea/markdown-navigator.xml | 78 +++++++++++ .../markdown-navigator/profiles_settings.xml | 3 + .idea/misc.xml | 6 + .idea/modules.xml | 9 ++ .idea/modules/course-build.iml | 104 +++++++++++++++ .idea/modules/course.iml | 22 ++++ .idea/sbt.xml | 18 +++ .idea/scala_compiler.xml | 6 + .idea/scala_settings.xml | 7 + .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + build.sbt | 7 + project/build.properties | 1 + src/docs/sprint.md | 106 +++++++++++++++ src/main/scala/Chapter1-Basic.sc | 47 +++++++ src/main/scala/Chapter2-Basic-2.sc | 73 +++++++++++ src/main/scala/Chapter3-Basic-3.sc | 15 +++ src/main/scala/Chapter4-Collection-1.sc | 64 +++++++++ .../Chapter5-matcher-function-compose.sc | 1 + src/main/scala/Test.scala | 5 + src/main/scala/test.md | 0 src/test.md | 30 +++++ src/test/scala/com/rhyme/MySpec.scala | 21 +++ src/test/scala/com/rhyme/UnitSpec.scala | 6 + 31 files changed, 837 insertions(+) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/hydra.xml create mode 100644 .idea/libraries/sbt__org_scala_lang_modules_scala_xml_2_12_1_0_6_jar.xml create mode 100644 .idea/libraries/sbt__org_scala_lang_scala_library_2_12_6_jar.xml create mode 100644 .idea/libraries/sbt__org_scala_lang_scala_reflect_2_12_6_jar.xml create mode 100644 .idea/libraries/sbt__org_scalactic_scalactic_2_12_3_0_5_jar.xml create mode 100644 .idea/libraries/sbt__org_scalatest_scalatest_2_12_3_0_5_jar.xml create mode 100644 .idea/markdown-navigator.xml create mode 100644 .idea/markdown-navigator/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/modules/course-build.iml create mode 100644 .idea/modules/course.iml create mode 100644 .idea/sbt.xml create mode 100644 .idea/scala_compiler.xml create mode 100644 .idea/scala_settings.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 src/docs/sprint.md create mode 100644 src/main/scala/Chapter1-Basic.sc create mode 100644 src/main/scala/Chapter2-Basic-2.sc create mode 100644 src/main/scala/Chapter3-Basic-3.sc create mode 100644 src/main/scala/Chapter4-Collection-1.sc create mode 100644 src/main/scala/Chapter5-matcher-function-compose.sc create mode 100644 src/main/scala/Test.scala create mode 100644 src/main/scala/test.md create mode 100644 src/test.md create mode 100644 src/test/scala/com/rhyme/MySpec.scala create mode 100644 src/test/scala/com/rhyme/UnitSpec.scala diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/hydra.xml b/.idea/hydra.xml new file mode 100644 index 0000000..66eeb9a --- /dev/null +++ b/.idea/hydra.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/.idea/libraries/sbt__org_scala_lang_modules_scala_xml_2_12_1_0_6_jar.xml b/.idea/libraries/sbt__org_scala_lang_modules_scala_xml_2_12_1_0_6_jar.xml new file mode 100644 index 0000000..3a9e344 --- /dev/null +++ b/.idea/libraries/sbt__org_scala_lang_modules_scala_xml_2_12_1_0_6_jar.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/sbt__org_scala_lang_scala_library_2_12_6_jar.xml b/.idea/libraries/sbt__org_scala_lang_scala_library_2_12_6_jar.xml new file mode 100644 index 0000000..de7eadd --- /dev/null +++ b/.idea/libraries/sbt__org_scala_lang_scala_library_2_12_6_jar.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/sbt__org_scala_lang_scala_reflect_2_12_6_jar.xml b/.idea/libraries/sbt__org_scala_lang_scala_reflect_2_12_6_jar.xml new file mode 100644 index 0000000..760254f --- /dev/null +++ b/.idea/libraries/sbt__org_scala_lang_scala_reflect_2_12_6_jar.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/sbt__org_scalactic_scalactic_2_12_3_0_5_jar.xml b/.idea/libraries/sbt__org_scalactic_scalactic_2_12_3_0_5_jar.xml new file mode 100644 index 0000000..c4b6485 --- /dev/null +++ b/.idea/libraries/sbt__org_scalactic_scalactic_2_12_3_0_5_jar.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/sbt__org_scalatest_scalatest_2_12_3_0_5_jar.xml b/.idea/libraries/sbt__org_scalatest_scalatest_2_12_3_0_5_jar.xml new file mode 100644 index 0000000..6126aa3 --- /dev/null +++ b/.idea/libraries/sbt__org_scalatest_scalatest_2_12_3_0_5_jar.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..076726f --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..57927c5 --- /dev/null +++ b/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d5d79e0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..822e6ee --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/course-build.iml b/.idea/modules/course-build.iml new file mode 100644 index 0000000..b7b61f8 --- /dev/null +++ b/.idea/modules/course-build.iml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules/course.iml b/.idea/modules/course.iml new file mode 100644 index 0000000..0c70460 --- /dev/null +++ b/.idea/modules/course.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/sbt.xml b/.idea/sbt.xml new file mode 100644 index 0000000..508ce00 --- /dev/null +++ b/.idea/sbt.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/scala_compiler.xml b/.idea/scala_compiler.xml new file mode 100644 index 0000000..d3f3203 --- /dev/null +++ b/.idea/scala_compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/scala_settings.xml b/.idea/scala_settings.xml new file mode 100644 index 0000000..d687081 --- /dev/null +++ b/.idea/scala_settings.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..8ba8cf3 --- /dev/null +++ b/build.sbt @@ -0,0 +1,7 @@ +name := "course" + +version := "0.1" + +scalaVersion := "2.12.6" + +libraryDependencies += "org.scalatest" % "scalatest_2.12" % "3.0.5" % "test" \ No newline at end of file diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..16eecf5 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.1.6 \ No newline at end of file diff --git a/src/docs/sprint.md b/src/docs/sprint.md new file mode 100644 index 0000000..6598171 --- /dev/null +++ b/src/docs/sprint.md @@ -0,0 +1,106 @@ +## 1.列举Scala的主要特性 + +### 1. 伴生对象以及apply()方法 +### 2. 变长参数 +### 3. 柯里化函数与部分应用 +### 4. Option +### 5. 函数组合子 +### 6. 偏函数 +### 7. 类型推断 +### 8. 函数是一等公民(函数和其他基本类型地位相等,可以做为参数传入,也可以作为返回值返回) + +## 2.静态类型、动态类型、强类型、弱类型如何理解 + +### 静态类型 +>在编译时就能确定的类型 +### 动态类型 +>泛型与多态 +### 强类型 +>不容忍隐式类型转换 +### 弱类型 +>容忍隐式类型转换 + +## 3.什么是纯函数/引用透明性 + +纯函数值的是函数和外界沟通的方式只有参数和返回值 + +以下的foo函数不是纯函数 + +```scala +class A { + private var a = 0 + + def foo() = { + a = a + 1 + 0 + } + } +``` + +## 4.简述数学意义上集合、映射、函数、域和值域的概念,以及编程意义上函数与其对应关系 + +### 4.1 数学意义上 + +>集合:存放元素的容器 + +>映射:f(x) = y + +>函数:f(x) = x^2 + +>域:定义域 + +>值域:函数的实际输出集合 + +### 4.2 编程意义上的函数意义 + +编程意义上的函数参数对应定义域,函数的返回值组成的集合对应数学中函数的值域 + +## 5. 简述函数式编程和指令式以及面向对象的关联、区别 + +### 5.1 函数式编程 + +>将运算过程定义为一系列嵌套的函数调用 + +#### 1. 函数是一等公民 +>函数和其他基本类型一样,地位是相等的,可以作为参数传入和返回 +#### 2. 只用表达式,不用语句 +>表达式是单纯的计算,总是有返回值,函数式编程要求只使用表达式,并可以得到每一步的返回值 +#### 3. 没有副作用 +>函数式编程强调纯函数,就是函数设计要独立,不能对外界造成影响 +#### 4. 不修改状态 +>函数不修改变量,变量负责保存状态 +#### 5. 依赖透明 +>函数的运行只与传入的参数相关,与外部不相关,相同的参数,应该得到相同的结果 + +#### 6. 函数式编程的好处 + +1. 代码简洁,开发迅速,代码重用度高 +2. 接近自然语言,利于理解 +3. 利于并发编程和热升级和代码管理(由于每一个函数都可以看做是一个独立的单元,因此也就能够很容易的执行管理) + +## 6. 简述高阶函数,高阶类型,类型构造器 + +### 6.1 高阶函数 +>接受函数参数或返回一个函数的函数,简单来说就是能够操纵其他函数 +### 6.2 高阶类型 +>和高阶函数同理,高阶类型值的是接受其他类型构造出来的新类型,可以用于简化类型 +### 6.3 类型构造器 +>类型构造器原理和函数相似,只不过类型构造器处理的是类型,在函数中,我传入一个a,返回一个b,在类型构造器中,我传入一个类型,返回一个类型 + +## 7. 什么是non-strict/strict + +### 7.1 strict + +>在调用函数时,先对参数进行计算再传入,会对计算过的参数进行缓存 + +### 7.2 non-strict + +>直接传入参数,在函数内部使用到参数时,再对参数进行计算,不会对计算过的参数进行缓存 + + + + + + + + diff --git a/src/main/scala/Chapter1-Basic.sc b/src/main/scala/Chapter1-Basic.sc new file mode 100644 index 0000000..86a5afc --- /dev/null +++ b/src/main/scala/Chapter1-Basic.sc @@ -0,0 +1,47 @@ +//匿名函数 +val addOne = (i: Int) => i + 1 +addOne(1) +def test2(i: Int): Int = { + print("test") + i + 1 +} +test2(2) + +//部分应用 +def add(m: Int, n: Int): Int = { + m * n +} + +val add2 = add(3, _: Int) +add2(3) + +//柯里化函数 +def multiBy(m: Int)(n: Int): Int = { + m * n +} + +multiBy(2)(3) + +val first = multiBy(2) _ +first(3) + +//可变长度参数 + +def testAll(args: String*): Seq[String] = { + args map (arg => arg.toUpperCase()) +} + +testAll(args = "string", "hello") + +//类 +class Person { + val name: String = "Rhyme" + + def add(m: Int, n: Int): Int = { + m + n + } +} + +val person = new Person +person.name + diff --git a/src/main/scala/Chapter2-Basic-2.sc b/src/main/scala/Chapter2-Basic-2.sc new file mode 100644 index 0000000..19eeba7 --- /dev/null +++ b/src/main/scala/Chapter2-Basic-2.sc @@ -0,0 +1,73 @@ +//apply方法 +class Person { + var name: String = "Hello" + + def apply(str: String): Person = { + new Person + } +} + +val person = new Person +person.name + +//单例对象 +object Timer { + var count = 0 + + def increase(): Int = { + count += 1 + count + } +} + +Timer.increase() +Timer.increase() + +class Student(_name: String) { + var name = _name +} + +object Student { + def apply(name: String): Student = new Student(name) +} + +val s = Student("Rhyme") +s.name + +//函数即是对象 +class addOne extends (Int => Int) { + def apply(m: Int): Int = m + 1 +} + +val a = new addOne +a(1) + +//模式匹配 +val times = 1 +times match { + case 1 => "One" + case 2 => "Two" + case 3 => "Three" + case _ => "Others" +} + +//带有守卫的模式匹配 +times match { + case i if i == 1 => "One" + case i if i == 2 => "Two" + case _ => "Others" +} + +//模式匹配匹配类型 + +def matchType(o: Any): Any = { + o match { + case i: Int => i + " Int" + case text: String => text + " String" + case _ => "Other Types" + } +} + +matchType(2) + + diff --git a/src/main/scala/Chapter3-Basic-3.sc b/src/main/scala/Chapter3-Basic-3.sc new file mode 100644 index 0000000..246b374 --- /dev/null +++ b/src/main/scala/Chapter3-Basic-3.sc @@ -0,0 +1,15 @@ +//样本类 +case class Person(name: String, age: Int) + +var person = Person("Rhyme", 30) +person.name + +//使用样本类进行模式匹配 +def matchPerson(person: Person): String = person match { + case Person("Rhyme", 30) => "young boy" + case Person("Rhyme", 10) => "Children" + case _ => "Others" +} + +matchPerson(Person("Rhyme", 30)) + diff --git a/src/main/scala/Chapter4-Collection-1.sc b/src/main/scala/Chapter4-Collection-1.sc new file mode 100644 index 0000000..5de3e7b --- /dev/null +++ b/src/main/scala/Chapter4-Collection-1.sc @@ -0,0 +1,64 @@ +// Array 可变 可以包含重复项 +val array = Array(1, 2, 3, 4) +array(1) = 5 +array +// Set 不可变 不包含重复项 +val set = Set(1, 2, 3, 4) +//set(1) = 5 + +//Tuple元组 +val tuple = ("Rhyme", 20) +tuple._1 +tuple._2 + +//元组很适合用在模式匹配上 + +val simple_tuple = 1 -> 2 + +//Option + +val map = Map("Rhyme" -> 20, "Chiang" -> 30) +val res = map.get("Rhyme") +map.getOrElse("Rhyme", -1) + +// Option类天然的适合模式匹配 +res match { + case Some(n) => n + case None => -1 +} + +//函数组句子 +val list = List(1, 2, 3, 4) +list map ((i: Int) => i * 2) + +def timesTwo(i: Int): Int = { + i * 2 +} + +list map timesTwo + +//foreach +list foreach ((i: Int) => i * 2) + +list + +list filter ((i: Int) => i > 0) + +//zip +val a = List(1, 2, 3, 4) +val c = List(5, 6, 7, 8) +val b = List("a", "b", "c", "d") +a.zip(b) + +//partition +list.partition(_ % 2 == 0) + +//find +list.find(_ % 2 == 0) + +//flatten +val f = List(a, b).flatten + +//flatten map +List(a, c).flatMap(x => x map (_ * 2)) + diff --git a/src/main/scala/Chapter5-matcher-function-compose.sc b/src/main/scala/Chapter5-matcher-function-compose.sc new file mode 100644 index 0000000..ed7a1d8 --- /dev/null +++ b/src/main/scala/Chapter5-matcher-function-compose.sc @@ -0,0 +1 @@ +// 模式匹配和函数组合 diff --git a/src/main/scala/Test.scala b/src/main/scala/Test.scala new file mode 100644 index 0000000..9f5ff43 --- /dev/null +++ b/src/main/scala/Test.scala @@ -0,0 +1,5 @@ +object Test { + def main(args: Array[String]): Unit = { + println("pppp") + } +} diff --git a/src/main/scala/test.md b/src/main/scala/test.md new file mode 100644 index 0000000..e69de29 diff --git a/src/test.md b/src/test.md new file mode 100644 index 0000000..94be2c1 --- /dev/null +++ b/src/test.md @@ -0,0 +1,30 @@ +## 列举Scala的主要特性 + +### 1. 伴生对象以及apply()方法 +### 2. 变长参数 +### 3. 柯里化函数与部分应用 +### 4. Option +### 5. 函数组合子 +### 6. 偏函数 +### 7. 类型推断åå + +## 静态类型、动态类型、强类型、弱类型如何理解 + +### 静态类型 +>在编译时就能确定的类型 +### 动态类型 +>泛型与多态 +### 强类型 +>不容忍隐式类型转换 +### 弱类型 +>容忍隐式类型转换 + + +## 什么是纯函数/引用透明性 + +纯函数值的是函数和外界沟通的方式只有参数和返回值 + + + + + diff --git a/src/test/scala/com/rhyme/MySpec.scala b/src/test/scala/com/rhyme/MySpec.scala new file mode 100644 index 0000000..2f1b7e9 --- /dev/null +++ b/src/test/scala/com/rhyme/MySpec.scala @@ -0,0 +1,21 @@ +package com.rhyme + +import collection.mutable.Stack +import org.scalatest._ + +class MySpec extends FlatSpec { + "A Stack" should "pop values in last-in-first-out order" in { + val stack = new Stack[Int] + stack.push(1) + stack.push(2) + assert(stack.pop() === 2) + assert(stack.pop() === 1) + } + + it should "throw NoSuchElementException if an empty stack is popped" in { + val emptyStack = new Stack[String] + assertThrows[NoSuchElementException] { + emptyStack.pop() + } + } +} diff --git a/src/test/scala/com/rhyme/UnitSpec.scala b/src/test/scala/com/rhyme/UnitSpec.scala new file mode 100644 index 0000000..ca23daa --- /dev/null +++ b/src/test/scala/com/rhyme/UnitSpec.scala @@ -0,0 +1,6 @@ +package com.rhyme + +import org.scalatest._ + +abstract class UnitSpec extends FlatSpec with Matchers with + OptionValues with Inside with Inspectors \ No newline at end of file