大数据系列-scala流程控制

来源:互联网 发布:java同步异步调用 编辑:程序博客网 时间:2024/05/18 04:01

Scala流程控制

if的使用

scala> val x= if("hello"=="hello") 1 else 0x: Int = 1

while的使用

scala>  def gcdloop(x:Long,y:Long): Long={            var a=x            var b=y            while (a !=0){            val temp=a            a=b%a            b=temp            }            b            }gcdloop: (x: Long, y: Long)Long

do while 的使用

var line =""do { line=readLine() println("Read: "+line)} while(line !="")

注意:与if不同的是,while与do while 不能用作表达式,也即其返回值为Unit,

//利用if替换while控制结构//这些做可以减少var变量的使用,程序结构也更简单,表达能力更强def gcd(x:Long,y:Long):Long=      if (y==0) x else gcd(y,x%y)gcd: (x: Long, y: Long)Long

//在scala中不能这么用,因为scala中的赋值操作返回的值是Unit,而”“是string类型,不能进行比较,这是函数式编程语言特有的特点

var line=""while((line =readLine()) !="")println("Read:"+line)scala> val filesHere=(new java.io.File(".")).listFiles//集合操作方式scala> for(file<-filesHere)     | println(file)//间接调用方式scala> for(i<- 0 to filesHere.length -1)     | println(filesHere(i))   

程序中的<- 被称生成器(generator),在执行过程中,集合filesHere中(Array[File])的元素将依次赋给file,
file类型为File,打印时调用其toString方法将文件名称打印出来

scala> val filesHere=(new java.io.File(".")).listFilesfilesHere: Array[java.io.File] = Array(./.gnupg, ./Videos, ./.gconf, ./.gstreamer-0.10, ./.gnome2, ./Documents, ./.vim, ./xululog, ./.esd_auth, ./zbx, ./.imsettings.log, ./.ICEauthority, ./bbb, ./.pulse, ./.ssh, ./.bash_history, ./anaconda-ks.cfg, ./.bash_profile, ./.dbus, ./Templates, ./.abrt, ./CDH-5.7.0-1.cdh5.7.0.p0.45-el6.parcel.sha, ./.cache, ./Desktop, ./expect1.sh, ./list, ./install.log.syslog, ./bin, ./.cshrc, ./.config, ./.bashrc, ./.bash_logout, ./clean.sh, ./aaaa, ./H_000017PHIS0000000002_ProCinsured_054.txt, ./Pictures, ./.lesshst, ./.gconfd, ./.viminfo, ./.gvfs, ./.mysql_history, ./patfile, ./install.log, ./expect.sh, ./.gnote, ./.gtk-bookmarks, ./test, ./.tcshrc, ./new.txt, ./.Xauthority, ./slogs, ./Public, ./.nautilus, ./shell, ./Music, ./.local, ./patter, ./test.sh, ./....scala> for(file<-filesHere)     | println(file)./.gnupg./Videos./.gconf./.gstreamer-0.10./.gnome2./Documents./.vim./xululog./.esd_auth./zbx./.imsettings.log./.ICEauthority./bbb./.pulse./.ssh./.bash_history./anaconda-ks.cfg./.bash_profile./.dbus./Templates./.abrt./CDH-5.7.0-1.cdh5.7.0.p0.45-el6.parcel.sha./.cache./Desktop./expect1.sh./list./install.log.syslog./bin./.cshrc./.config./.bashrc./.bash_logout./clean.sh./aaaa./H_000017PHIS0000000002_ProCinsured_054.txt./Pictures./.lesshst./.gconfd./.viminfo./.gvfs./.mysql_history./patfile./install.log./expect.sh./.gnote./.gtk-bookmarks./test./.tcshrc./new.txt./.Xauthority./slogs./Public./.nautilus./shell./Music./.local./patter./test.sh./.pulse-cookie./.scala_history./Downloads./data

<-生成器对其它类型的集合也适用

scala> 1 to 5res3: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)scala> for(i<- res3) println("Iteration"+i)Iteration1Iteration2Iteration3Iteration4Iteration5scala> for(i<- 1 to 5) println("Iteration"+i)Iteration1Iteration2Iteration3Iteration4Iteration5//如果不需要5出现,则用scala> for(i<- 1 until 5) println("Iteration"+i)Iteration1Iteration2Iteration3Iteration4

在for循环结构中还可以加入if进行过滤操作

scala> val filesHere =(new java.io.File(".")).listFilesscala> for(file <- filesHere if file.getName.endsWith(".sh"))     | println(file)./expect1.sh./clean.sh./expect.sh./test.sh

//还可以加入多个过滤条件,用;隔开

scala> for(file <- filesHere if file.getName.endsWith(".sh");if file.isFile)     | println(file)./expect1.sh./clean.sh./expect.sh./test.sh

//多重循环的实现:

def fileLines(file: java.io.File) =scala.io.Source.fromFile(file).getLines.toListdef grep(pattern: String) =for (file <- filesHereif file.getName.endsWith(".sh");line <- fileLines(file)if line.trim.matches(pattern)) println(file +": "+ line.trim)grep(".*sh")//前一个for相当于下列语句def grep(pattern: String) =for (file <- filesHereif file.getName.endsWith(".sh")) for(line <- fileLines(file)if line.trim.matches(pattern))println(file +": "+ line.trim)grep(".*sh")
0 0
原创粉丝点击