加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala基础

发布时间:2020-12-16 09:40:03 所属栏目:安全 来源:网络整理
导读:[root @ivan 桌面] # scala Welcome to Scala 2.11 .8 (Java HotSpot(TM) 64 -Bit Server VM,Java 1.8 .0 _92).Type in expressions for evaluation. Or try :help.scala val one :Int = 1 one : Int = 1 scala one res0 : Int = 1 scala val two = 2 two :
[root@ivan 桌面]# scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM,Java 1.8.0_92).
Type in expressions for evaluation. Or try :help.

scala> val one:Int = 1
one: Int = 1

scala> one
res0: Int = 1

scala> val two = 2
two: Int = 2

scala> two
res1: Int = 2

scala> 6159+7430
res2: Int = 13589

scala> res2/3
res3: Int = 4529

scala> def addOne(num: Int):Int = {return num + 1}
addOne: (num: Int)Int

scala> print(addOne(2))
3

scala> print(addOne(2))
3
scala> def addOne(num: Int):Int = {num + 1}
addOne: (num: Int)Int

scala> print(addOne(2))
3

scala> def addTwo(num: Int){num + 2}
addTwo: (num: Int)Unit

scala> addTwo(1)

scala> print(addTwo(1))
()
scala> def printTwo(){print(2)}
printTwo: ()Unit

scala> printTwo
2
scala> 

scala> def printTwo(){print(2)}
printTwo: ()Unit

scala> printTwo
2
scala> def addOne(num: Int):Int = {num + 1}
addOne: (num: Int)Int

scala> print(addOne(1))
2
scala> Display all 409 possibilities? (y or n)

scala> 1 until 10
res13: scala.collection.immutable.Range = Range(1,2,3,4,5,6,7,8,9)

scala> Range(1,10,3)
res14: scala.collection.immutable.Range = Range(1,7)

scala> 1 to 10
res15: scala.collection.immutable.Range.Inclusive = Range(1,9,10)

scala> for(i <- 1 to 10)println(i)
1
2
3
4
5
6
7
8
9
10

scala> for(i <- 1 to 10 if(i%2==0)) println(i)
2
4
6
8
10

scala> def bianchang(x: Int*)=println(x)
bianchang: (x: Int*)Unit

scala> bianchang(1)
WrappedArray(1)

scala> bianchang(1,3)
WrappedArray(1,3)

scala> def add(x:Int=1,y:Int=2)=print(x+y)
add: (x: Int,y: Int)Unit

scala> add(1)
3
scala> add(3)
5
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val ab = Arr
Array         ArrayCharSequence                ArrayStoreException   
ArrayBuffer   ArrayIndexOutOfBoundsException   ArrowAssoc            

scala> val ab = Array
Array         ArrayCharSequence                ArrayStoreException   
ArrayBuffer   ArrayIndexOutOfBoundsException                         

scala> val ab = ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> ab+=(1,4)
res22: ab.type = ArrayBuffer(1,4)

scala> ab++=Array(1,4)
res23: ab.type = ArrayBuffer(1,1,4)

scala> ab.sum
res25: Int = 20

scala> ab.max
res26: Int = 4

scala> ab.toArray
res27: Array[Int] = Array(1,4)

scala> ab.reverse
res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4,1)

scala> val m = Map("Tom"->10,"Lucy"->9)
m: scala.collection.immutable.Map[String,Int] = Map(Tom -> 10,Lucy -> 9)

scala> val m = Map()
m: scala.collection.immutable.Map[Nothing,Nothing] = Map()

scala> val m = scala.collection.mutable.Map()
m: scala.collection.mutable.Map[Nothing,Nothing] = Map()

scala> m += ("t"->1)
<console>:14: error: type mismatch;
 found   : (String,Int)
 required: (Nothing,Nothing)
       m += ("t"->1)
                ^

scala> val m = scala.collection.mutable.Map[String,Int]()
m: scala.collection.mutable.Map[String,Int] = Map()

scala> m += ("t"->1)
res30: m.type = Map(t -> 1)

scala> m
res31: scala.collection.mutable.Map[String,Int] = Map(t -> 1)

scala> m.getOrElse("t",0)
res32: Int = 1

scala> m.getOrElse("tt","0")
res33: Any = 0

scala> m += ("c"->2)
res34: m.type = Map(t -> 1,c -> 2)

scala> m -= ("c"->2)
<console>:14: error: type mismatch;
 found   : (String,Int)
 required: String
       m -= ("c"->2)
                ^

scala> m -= ("c")
res36: m.type = Map(t -> 1)

scala> val _add = add _
_add: (Int,Int) => Unit = <function2>

scala> _add(1,2)
3
scala> (x: Int,y: Int) => x+y
res38: (Int,Int) => Int = <function2> #function2有两个参数的函数

scala> res38(1,2)
res39: Int = 3

scala> val l = List(1,3)
l: List[Int] = List(1,3)

scala> l.map(_*2)
res40: List[Int] = List(2,6)

scala> def fun(f: (Int,Int)=>Int):Int=f(1,2) fun: (f: (Int,Int) => Int)Int scala> fun((1,2)) <console>:14: error: type mismatch; found : (Int,Int) required: (Int,Int) => Int
       fun((1,2))
           ^

scala> => function
<console>:1: error: illegal start of definition => function
^

scala> fun(res38(1,2))
<console>:15: error: type mismatch;
 found   : Int
 required: (Int,Int) => Int
       fun(res38(1,2))
                ^

scala> fun(res38)
res43: Int = 3

scala> def m(x: Int)=(y: Int)=>x+y
m: (x: Int)Int => Int

scala> def add(x: Int)=x+1
add: (x: Int)Int

scala> m(1)
res44: Int => Int = <function1>

scala> res44(2)
res45: Int = 3

scala> m(1)(2)
res46: Int = 3

scala> val l = List(1,4)
l: List[Int] = List(1,4)

scala> l.map(_*2).filter(_>3)
res47: List[Int] = List(4,8)

scala> l.reduce(_+_)
res48: Int = 10

scala> l.reduce((x,y)=>x+y) res49: Int = 10 scala> l.fold(100)(_+_) res50: Int = 110 scala> def add(x: => Int) = x add: (x: => Int)Int scala> add(1) res51: Int = 1 scala> lazy val x = 1 x: Int = <lazy> scala> def fun(b: () => Unit){b()} fun: (b: () => Unit)Unit scala> fun(()=>println("x")) x scala> def fun(b: => Unit){b} fun: (b: => Unit)Unit scala> fun(()=>println("x")) scala> fun(println("x")) x scala> sc.textFile("/root/scala_shell1").count res1: Long = 541 scala> val wc = sc.textFile("/root/scala_shell1").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_) wc: org.apache.spark.rdd.RDD[(String,Int)] = ShuffledRDD[6] at reduceByKey at <console>:24 scala> wc.foreach(println) (scala,2) (package,1) scala> sc.textFile("/root/scala_shell1").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).take(2) res4: Array[(String,Int)] = Array((scala,2),(package,1)) scala> sc.textFile("/root/scala_shell1").flatMap(_.split(" ")).map(word=>(word,1)).reduceByKey(_+_).take(2) res5: Array[(String,1)).map(t=>(t._2,t._1)).take(2) res6: Array[(Int,String)] = Array((1,[root@ivan),(1,桌面]#)) scala> sc.textFile("/root/scala_shell1").flatMap(_.split(" ")).map(word=>(word,1)).reduceByKey(_+_).map(t=>(t._2,t._1)).sortByKey(false).take(2) res12: Array[(Int,String)] = Array((4802,""),(98,scala>)) 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读