Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
upgrade version to scala2.13.5, rust1.50,kotlin1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Mar 6, 2021
1 parent ffea290 commit e034c3a
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@

### 环境

- Scala 2.12.12
- Scala 2.13.5
- Java 1.8
- Python 3.0+
- Rust 1.4+
- Rust 1.50.0
- Kotlin 1.3+
- C++ 11
- Ruby 2.5+
Expand Down
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ allprojects {
apply plugin: 'com.diffplug.gradle.spotless'

group = 'io.github.dreamylost'
version = '2.0.0'
version = '2.0.1'

description = "Recording Learning, Work, Interview, Summary and Reflection"

Expand All @@ -37,6 +37,8 @@ allprojects {
// name "aliyungoogle"
// url "https://maven.aliyun.com/repository/google"
// }
mavenLocal()

maven {
name "aliyunmaven"
url "https://maven.aliyun.com/nexus/content/groups/public/"
Expand Down Expand Up @@ -71,8 +73,8 @@ allprojects {
//because some test write in src/main/java
compile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.12'
compile group: 'org.scala-lang', name: 'scala-reflect', version: '2.12.12'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.13.5'
compile group: 'org.scala-lang', name: 'scala-reflect', version: '2.13.5'
compile group: 'com.diffplug.spotless', name: 'spotless-plugin-gradle', version: '4.5.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Fri Jan 17 14:58:17 CST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
6 changes: 3 additions & 3 deletions kotlin-leetcode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version = '0.0.1'
group = 'io.github.dreamylost'

buildscript {
ext.kotlin_version = '1.3.71'
ext.kotlin_version = '1.4.31'

//编译插件的依赖仓库
repositories {
Expand Down Expand Up @@ -31,12 +31,12 @@ configurations {
sourceSets {
main {
kotlin {
srcDirs = ['src/main/kotlin']
srcDirs += 'src/main/kotlin'
}
}
test {
kotlin {
srcDirs = ['src/test/kotlin']
srcDirs += 'src/test/kotlin'
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions kotlin-study/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version = '0.0.1'
group = 'io.github.dreamylost'

buildscript {
ext.kotlin_version = "1.3.71"
ext.kotlin_version = "1.4.31"

//编译插件的依赖仓库
repositories {
Expand Down Expand Up @@ -31,12 +31,12 @@ configurations {
sourceSets {
main {
kotlin {
srcDirs = ['src/main/kotlin']
srcDirs += 'src/main/kotlin'
}
}
test {
kotlin {
srcDirs = ['src/test/kotlin']
srcDirs += 'src/test/kotlin'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion scala-examples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dependencies {
compile group: 'com.typesafe.play', name: 'play_2.12', version: '2.7.0'
compile group: 'com.typesafe.play', name: 'play_2.13', version: '2.7.9'
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Construction4 @SerialVersionUID(1L) (override val var1: String, override v
// (3)当你在某个类或者trait X 里面看到super.method的时候,你想知道实际被执行的是哪个类或者trait里的method:
// 你首先要知道这个super所指向的对象的“实际类型”,然后做线性化,然后,从线性化得到的“链”里,从X开始往左边找(不包括X本身),首先找到的那个method就是实际被执行的方法。
// 需要注意的是上述是针对特质,抽象类仍然是单继承
def this() {
def this() = { //2.13.5 必须加等号
this(null, null) //使用空构造,也需要调用主构造并传入null,因为默认自己写了有参的主构造,就不再提供无参主构造
super.print("hello world") //排除自身从左开始就是:Construction3类
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Rantional(val n: Int, val d: Int) {

// println(n + "/" + d) //构造对象时执行
//重写非抽象方法,必须加override
override def toString: String = n + "/" + d
override def toString: String = s"$n/$d" //2.13 数字不能和字符串相加

def this(n: Int) {
def this(n: Int) = { //2.13.5 必须加等号
this(n, 1)
}

Expand Down Expand Up @@ -49,7 +49,7 @@ object TestRantional extends App {
println(ret1) // 输出1/4
println("================重载方法===============")
println(ret.test("name"))
println(ret.test)
println(ret.test())
println("================隐式转换===============")
val n = new Rantional(2, 2)
println(n * 3) // 输出2*3/2*3 //这里不需要隐式转换等同 n.*(3)
Expand Down Expand Up @@ -168,6 +168,7 @@ object Function3 extends App {

val arr = Array("hello", "world")
// prt(arr)//编译报错
// prt(arr: _*) //OK,告诉编译器将arr的每个元素作为参数传进去,而不是将arr作为一个整体
prt(arr: _*) //OK,告诉编译器将arr的每个元素作为参数传进去,而不是将arr作为一个整体

//带名字参数,字面量/匿名函数不能使用带名参数
Expand Down
18 changes: 11 additions & 7 deletions scala-examples/src/main/scala/io/github/dreamylost/IOBasic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package io.github.dreamylost

import scala.io.Source
import scala.util.Using

/**
* 从文件读取行,文件写入使用Java IO
Expand All @@ -11,15 +12,18 @@ import scala.io.Source
*/
object IOBasic {

val file =
"D:\\git_project\\cs-summary-reflection\\src\\cn\\edu\\jxnu\\scala\\basic\\IOExamples.scala"
val liness = Source.fromFile(file).getLines().toList
val file = "README.md"
val lines = Using
.apply(Source.fromFile(file)) {
_.getLines().toList
}
.getOrElse(List.empty)

def main(args: Array[String]): Unit = {
//格式化打印
val maxWidth = widthOfLength(longestLine)

for (line <- liness) {
for (line <- lines) {
val nums = maxWidth - widthOfLength(line)
val padding = " " * nums
println(padding + line.length + " | " + line)
Expand All @@ -35,15 +39,15 @@ object IOBasic {
/**
* 计算最大长度 函数式方法
*/
val longestLine = liness.reduceLeft((a, b) => if (a.length > b.length) a else b)
val longestLine = lines.reduceLeft((a, b) => if (a.length > b.length) a else b)

/**
* 计算最大长度 普通函数
*/
def longestLine2() {
def longestLine2() = { //2.13.5 必须加等号

var maxWidth = 0
for (line <- liness) {
for (line <- lines) {
maxWidth = maxWidth.max(widthOfLength(line))
}
}
Expand Down
19 changes: 11 additions & 8 deletions scala-examples/src/main/scala/io/github/dreamylost/LoopBasic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ object LoopBasic {

println("//差集")
for (i <- list.diff(list2)) { //--
print(i + " ")
print(s"$i ")
}
println()

println("//交集")
for (i <- list.intersect(list2)) { //&
print(i + " ")
print(s"$i ")
}
println()

println("//并集")
for (i <- list.union(list2).distinct) {
for (i <- list.concat(list2).distinct) {
// ++ | distinct去重
print(i + " ")
print(s"$i ")
}
println()

Expand All @@ -63,7 +63,7 @@ object LoopBasic {
println("breakable语句块来实现break操作")
breakable {
for (elem <- list)
if (elem < 0) break else println(elem)
if (elem < 0) break() else println(elem)
}
}

Expand All @@ -79,7 +79,7 @@ object LoopBasic {
println("breakable语句块来实现continue操作")
for (elem <- list)
breakable {
if (elem < 0) break else println(elem)
if (elem < 0) break() else println(elem)
}
}

Expand All @@ -93,7 +93,7 @@ object LoopBasic {
println("递归函数重写循环")

def next(i: Int): Unit = {
if (i >= list.size) Unit
if (i >= list.size) ()
else if (list(i) < 0) next(i + 1)
else println(list(i))
next(i + 1)
Expand All @@ -104,7 +104,10 @@ object LoopBasic {

//Scala赋值语句返回的不是赋值的那个值而是()
var line = ""
while ((line = readLine()) != "") {
while ({
line = scala.io.StdIn.readLine()
line
} != "") {
//这样永远是()!=“”
println(line)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ object MainObject1 extends App {
println("方法调用6 =>>>>>> " + default6)
}

getData
getData()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ package io.github.dreamylost
*/
object MainObject2 extends App {

MainObject1.getData
MainObject1.getData()

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object ScalaRDD extends App {
*/

//1,对数据进行合并的实现 union ++
val data: Array[(String, Double)] = d1 union d2 ++ d3
val data: Array[(String, Double)] = (d1 concat d2 ++ d3)
println("=========================union===========================")
data.foreach(println)

Expand All @@ -39,7 +39,7 @@ object ScalaRDD extends App {

println("=========================mapValues===========================")
//1.利用mapvalues算子,分别统计总的温度,以及月份的次数,然后求得平均温度
grouped
grouped.view
.mapValues(t => {
val totalSum = t.map(_._2).sum
val len = t.length
Expand All @@ -49,7 +49,7 @@ object ScalaRDD extends App {

println("=========================foldLeft===========================")
//2.利用foldLeft来实现,需要注意的是,因为初始值类型是Double,而元素类型是元组类型,所以这里不能用fold实现
grouped
grouped.view
.mapValues(t => {
val sum = t.foldLeft(0d)(_ + _._2)
sum / t.length
Expand All @@ -59,17 +59,18 @@ object ScalaRDD extends App {
println("=========================reduceLeft===========================")
//3.利用reduce或者reduceLeft 实现。因为reduce和reduceLeft的特性,这里的元素是元组类型,要求返回值类型也得是元组类型。
//所以,需要组装成元组,再取第二个元素。即为温度总值。然后再除以长度,得到结果值。
grouped
grouped.view
.mapValues(t => {
t.reduceLeft((a, b) => ("", a._2 + b._2))._2 / t.length
})
.foreach(println)

println("=========================aggregate===========================")
//4.利用aggregate实现。同样需要传递一个Double类型的初始值,然后进行统计计算。
grouped
grouped.view
.mapValues(t => {
t.aggregate(0d)(_ + _._2, _ + _) / t.length
// method aggregate in trait IterableOnceOps is deprecated (since 2.13.0): `aggregate` is not relevant for sequential collections. Use `foldLeft(z)(seqop)` instead.
t.foldLeft(0d)((b, seqop) => b + seqop._2) / t.length
})
.foreach(println)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.github.dreamylost

//待补
object Singleton {
def main(args: Array[String]) {
def main(args: Array[String]) = {
SingletonObject.hello()
}
}
Expand All @@ -24,7 +24,7 @@ object Singleton {
// 接着函数跳转到Singleton$.class 的 main 方法中,然后执行 Predef..MODULE$.hello()。
//---------------------
object SingletonObject { //object就是单例的
def hello() {
def hello() = {
println("Hello, This is a Singleton Object")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package io.github.dreamylost

object Test1 {
// scala 数组下标用(),泛型用[]
def main(args: Array[String]) {
def main(args: Array[String]) = {
println(apply(layout, 10))
useLambda()
}

// 函数 f 和 值 v 作为参数,而函数 f 又调用了参数 v
def apply(f: Int => String, v: Int) = f(v)

def layout[A](x: A) = "[" + x.toString() + "]"
def layout[A](x: A) = "[" + x.toString + "]"

//声明一个lambda
//实现对传入的两个参数进行凭借,lambda可以当做def使用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Test4 extends App {
Console println i //省略括号需要显示的给出方法调用的目标对象才有效,此时println是操作符
//实际上在Scala中任何操作符都是一种方法调用,而任何方法也可以是操作符,但是在多参数的情况下,操作符表示法必须用括号:strings indexOf ('a',startIndex)
val s = "hello"
s toLowerCase; //无参,无副作用不用括号,使用后缀需要隔断,用分号
s.toLowerCase; //无参,无副作用不用括号,使用后缀需要隔断,用分号,2.13.5禁止
println() //有副作用用括号
var num = -1 //-是前缀操作符,实际也是方法调用 可用的前缀操作符:! + - ~ 都是一元的
var num2 = 1.unary_- // unary_是混合操作符 yield在Scala是关键字,需要使用反引号`yield`,其他如match类似
Expand Down
12 changes: 6 additions & 6 deletions scala-examples/src/main/scala/io/github/dreamylost/Test8.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Test8 extends App {
println(set1.contains("hhh"))
println("===============Set遍历=============")
set1.foreach(print) //遍历
println
println()
println("================Map集===========")
//创建并初始化可变的Map
val map = collection.mutable.Map[String, String]()
Expand All @@ -27,18 +27,18 @@ object Test8 extends App {
println(map("b"))
println("=============单独获取key、value============")
//获取key
val keys = map.map(_._1) //前面一个占位符表示map的任意元素,后面一个表示任意元素的第一个元素,即 key
val keys = map.keys //前面一个占位符表示map的任意元素,后面一个表示任意元素的第一个元素,即 key
keys.foreach(print)
println
println()
//获取value
val valus = map.map(_._2) //类似
val valus = map.values //类似
valus.foreach(print)
println
println()
println("===========将两个list转化为一个map===============")
//反向操作可以使用zip,其中一个list作为key,另一个作为value
val list = List(1, 2, 3, 4)
//以少的为基准,不够舍弃
val scores = list.zip(valus).toMap //list作为key,values作为value
scores foreach { case (k, v) => println(k + v) }
scores foreach { case (k, v) => println(s"$k$v") }

}
Loading

0 comments on commit e034c3a

Please sign in to comment.