简单练习scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import scala.collection.immutable.Nil

object {
def main(args:Array[String]):Unit={

//ye
//jump
//testYield
//testArrayBuffer
//nest
//testPerson
//println(eletricity(20))
//println(eletricity(4))
//println(eletricity(10))
//dynamicPar(5,2)
//println(factorial(3))
/* var n = 0
for(n<-0 to 10){
print(getFib(n)+" ")
}*/

//println(giveSummer(500))
//(f1(ff,5,4))
//println(f1(_*_,5,4))
//println(myAdd(4,5))
//println(sub(7)(5))
//println(myFun(5))
//testOption
//testList
//testMatch
println ("Apply 方法 : " + apply("Zara", "gmail.com"));
println ("Unapply 方法 : " + unapply("[email protected]"));
println ("Unapply 方法 : " + unapply("Zara Ali"));

}
// 注入方法 (可选)
def apply(user: String, domain: String) = {
user +"@"+ domain
}

// 提取方法(必选)
def unapply(str: String): Option[(String, String)] = {
val parts = str split "@"
if (parts.length == 2){
Some(parts(0), parts(1))
}else{
None
}
}


def testMatch():Unit={
val p1:Person = new Person()
val p2:Person = Person("zhangsan",56)
val p3:Person = Person("lisi",28)
val p4:Person = Person("zhaoliu",25)
val plists :List[Person] = p1::p2::p3::Nil
val plists2 = List(p1,p2,p3)
println(test(3))
//println(test(p2))
//println(test(p3))
//println(test(p4))
println(plists)
//println(plists2)

println(plists.foreach(x =>test(x)))
//println(plists2.foreach(x =>test(x)))

def (x:Any):Any = x match {
case p:Person => s"p.name = ${p.name}t p.age = ${p.age}t p1.height = ${p.height}"
case p2:String => s"this is is $p2"
case p3:Int => p3+p3
case _ => s"This is error"
}

}

def testList():Unit={
val list1 = "deng"::"li"::"mao"::Nil
var list2 = Nil
val list3 = "abc" ++ list1

list3.foreach(println)
}

import java.util._
def testOption():Unit={
val name : Optional[String] = Optional.ofNullable(null)
if(name.isPresent){
println(name.get())
}else{
println("值为空")
}
}

var num = 5

def myFun(x:Int):Int = x*num

def sub(x:Int)(y:Int):Int = x-y

var myAdd = (x:Int,y:Int) => x+y

def f1(f2:(Int,Int)=>Double,x:Int,y:Int):Double = f2(x,y)

def ff(x:Int,y:Int):Double = x.toDouble+y.toDouble

def giveSummer:Double=>Double = partial(100,_:Double)
def partial(i: Int, i1: Double): Double ={
i+i1
}

import scala.collection.mutable.Set
def division(num:Int): Set[Int] ={
var set :Set[Int] = Set(1,2,3)
var i = 0
for(i <- 1 to num if (i%3)==0 if (i&1)==0) set += i
set
}

import scala.util.control._
def ye():Unit={
var i=0
var loop = new Breaks;
loop.breakable{
for (i<-0 to 14){
var j = i / 2

if(j==6){
loop.break
}
print(j+" ")
}
}
}

def jump(): Unit ={
var i = 0
var count = 0
for(i<- 1 to 100){
print(i+"t")
count += 1
if(count==10){
println
count=0
}
}
}

import scala.collection.immutable.List
def testYield():Unit={
var list = List(1,2,3,4,5,6,7,8,9,10,11,12)
var i = 0
val lists =
for {
i <- list
if (i&1)==0
if i%3==0
} yield i

lists.foreach(println)

}

import scala.collection.mutable._
def testArrayBuffer():Unit={
var sb = new ArrayBuffer[Int](5)
sb = (1+:2+:3+:4+:5+:sb)
var list = List(10,20,30,40)
//sb = (list ++: sb)
//sb.foreach(println)
sb = sb ++ list
sb = sb - 40
sb.update(0,222)
sb.foreach(println)

}

def nest():Unit={
val f1 = (x:Int) => x*x
def f2(i:Int ,f:Function[Int,Int]): Int ={
var j = i
j *= 10
f(j)
}

val res = f2(5,f1)
println(res)
}

def testPerson():Unit={
var p = new Person("maoge",18)
println(p.name+"---"+p.age+"---"+p.height)
var p2 = new Person();
println(p2.name+"---"+p2.age+"---"+p2.height)
println(p2.toString())
p2.play("maogg")
}

def eletricity(time:Int):Double={
def stage1:Double=0.5
def stage2:Double=0.3
def stage3:Double=0.2
if(time>=18&&time<=24){
stage1
}else if(time>=1&&time<6){
stage3
}else{
stage2
}
}


def dynamicPar(x:Int=3,y:Int=4):Unit={
println(x*y)
}

def factorial(n:Int=5):Int={
if(n == 1){
1
}else{
factorial(n-1)*n
}
}

def getFib(n:Int=7):Int={
if(n==0){
0
}else if(n==1){
1
}else{
getFib(n-1)+getFib(n-2)
}
}

}

class Person(val name:String,var age:Int ){
var height:Double = 175.0

def this(){
this("maomao",20)
this.height = 177
}

override def toString():String ={
this.name+"t"+this.age+"t"+this.height
}

def play(str:String):Unit={
println(str+" go to swimming.")
}


}

object Person{
def apply( name: String, age: Int): Person = new Person(name ,age)
def apply: Person = new Person
}

limaodeng

scribble