自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Scala 2.8的for表達(dá)式:性能與運(yùn)行順序的改進(jìn)

開(kāi)發(fā) 后端
Scala 2.8將是Scala語(yǔ)言的一次重要更新,感興趣的開(kāi)發(fā)者們已經(jīng)可以在通過(guò)其Nightly Build進(jìn)行很好的體驗(yàn)(各個(gè)IDE也已經(jīng)支持)。本文介紹了Scala 2.8對(duì)for表達(dá)式的改進(jìn):性能更好,運(yùn)行順序也進(jìn)行了調(diào)整。

本文來(lái)自EastSun的博客,原文標(biāo)題為《Scala2.8探秘之for表達(dá)式》。

51CTO編輯推薦:Scala編程語(yǔ)言專題

雖然Scala2.8還在持續(xù)跳票中,但目前Nightly Build版本的可用性已經(jīng)很高了,其中Scala2.8中主要特性都已經(jīng)實(shí)現(xiàn)。毫無(wú)疑問(wèn),Scala2.8的發(fā)布將會(huì)是Scala發(fā)展中的一個(gè)重大里程碑。在這個(gè)版本中,不僅包括了許多Scala社區(qū)期待已久的特性,如命名參數(shù)、類型特殊化等等,詳細(xì)的信息可以參看http://eastsun.javaeye.com/blog/373710;而且包含了一些對(duì)之前Scala中設(shè)計(jì)不合理的地方的改進(jìn),例如Scala中對(duì)String以及數(shù)組的處理,在之前的版本中Scala編譯器在編譯的時(shí)候?qū)@兩種類型進(jìn)行了一些比較特殊(魔法)的處理,這樣做雖然某些程度上使得這兩種類型在Scala中更加易用,但同時(shí)也破壞了Scala中的一致性,并隨之產(chǎn)生了一些離奇的問(wèn)題。舉個(gè)例子,在Scala2.7.7 final中我們可以看到如下結(jié)果:

  1. //Scala2.7.7 final  
  2. scala> val array = Array("String Array")  
  3. array: Array[java.lang.String] = Array(String Array)  
  4.  
  5. scala> println(array.toString())  
  6. Array(String Array)  
  7.  
  8. scala> println(array)  
  9. [Ljava.lang.String;@139491b 
  10.  
  11. scala> "WOW" == "WOW".reverse  
  12. res4: Boolean = false 
  13.  
  14. scala> "abcdefg" map { _.toUpperCase }  
  15. res5: Seq[Char] = ArrayBufferRO(A, B, C, D, E, F, G)         //注意,得到的是個(gè)Seq[Char]而不是String  
  16.  

顯然這樣的運(yùn)行結(jié)果有違我們的直覺(jué),即使是對(duì)Scala有一定了解的人也未必能馬上明白其中的奧妙。

在Scala2.8中,這些問(wèn)題都得到了一定的解決——可能有些解決方式會(huì)讓你覺(jué)得退步了,但是保持了Scala的一致性,并且消除了編譯器在背后做的那些魔法,使事情變得簡(jiǎn)單——雖然這樣破壞了Scala的向后兼容性,但我認(rèn)為這樣做事非常值得的:這為Scala成為一門(mén)成功的語(yǔ)言墊下了基礎(chǔ)。下面我們看看在Scala 2.8.0.r20117-b20091213020323中上面代碼的運(yùn)行結(jié)果:

  1. scala> val array = Array("String Array")  
  2. array: Array[java.lang.String] = Array(String Array)  
  3.  
  4. scala> println(array.toString())  
  5. [Ljava.lang.String;@335053 
  6.  
  7. scala> println(array)  
  8. [Ljava.lang.String;@335053 
  9.  
  10. scala> "WOW" == "WOW".reverse  
  11. res2: Boolean = true 
  12.  
  13. scala> "abcdefg" map { _.toUpperCase }  
  14. res3: String = ABCDEFG  
  15.  

可以看到,運(yùn)行結(jié)果就如我們所預(yù)想的那樣——固然其中數(shù)組的toString結(jié)果變得丑陋了,和Java中一樣丑陋,但保持一致了。

OK,String與數(shù)組的討論就此為止,以后如果有時(shí)間我再來(lái)詳細(xì)解釋一下其背后的故事;現(xiàn)在我們轉(zhuǎn)向本文要討論的東東:Scala中的for表達(dá)式。注意:在本文中使用了兩種不同的Scala版本:Scala2.7.7final與2.8.0.r20117-b20091213020323進(jìn)行對(duì)比。

1.Scala2.8之前的for表達(dá)式

在Scala中,通常有以下幾種使用方式:

  1. for (p <- e) e'  
  2. for (p <- e if g) e'  
  3. for (p <- e; p' <- e' ...) e'' 

以及相應(yīng)的

  1. for (p <- e) yield e'  
  2. for (p <- e if g) yield e'  
  3. for (p <- e; p' <- e' ...) yield e'' 

其中p,p'為Scala中的Pattern;e,e',e''為表達(dá)式;g為Boolean表達(dá)式。

根據(jù)《The Scala Language Specification Version 2.7》,上面的for表達(dá)式將在編譯階段展開(kāi)為下面的形式(沒(méi)有考慮p為比較復(fù)雜的Pattern時(shí)的情形):

  1. for (p <- e) e'                 => e.foreach { case p => e' }  
  2. for (p <- e if g) e'            => for (p <- e.filter{ (x1,...,xn) => g }) e' => ..  
  3. for (p <- e; p' <- e' ...) e''  => e.foreach{ case p => for (p' <- e' ...) e'' }  

以及相應(yīng)的

  1. for (p <- e) yield e'                => e.map { case p => e' }  
  2. for (p <- e if g) yield e'           => for (p <- e.filter{ (x1,...,xn) => g }) yield e' 
  3. for (p <- e; p' <- e' ...) yield e'' => e.flatmap { case p => for (p' <- e' ...) yield e'' }  

注意的是,這個(gè)轉(zhuǎn)換發(fā)生在類型檢查之前。也就是說(shuō),對(duì)map,filter,flatMap以及foreach這四個(gè)方法的方法簽名沒(méi)有任何其它限制,只需要滿足展開(kāi)后for語(yǔ)句的類型檢查。通常情況下,對(duì)于一個(gè)具有類型參數(shù)A的類C——一般表示某種數(shù)據(jù)結(jié)構(gòu)(集合)——下面的定義方式是比較自然的:

  1. class C[A] {  
  2.     def map[B](f: A => B): C[B]  
  3.     def flatMap[B](f: A => C[B]): C[B]  
  4.     def filter(p: A => Boolean): C[A]  
  5.     def foreach(b: A => Unit): Unit  
  6. }  
  7.  

相對(duì)Java1.5中的for語(yǔ)句,Scala的實(shí)現(xiàn)更加靈活,并且以一種輕量級(jí)的方式實(shí)現(xiàn)了List comprehension。舉個(gè)例子,下面幾行代碼實(shí)現(xiàn)了求一個(gè)List的全排列:

  1. scala> def perm[T](ls: List[T]): List[List[T]] = ls match {  
  2.      |     case Nil => List(Nil)  
  3.      |     case xs  => for(x <- xs;ys <- perm(xs-x)) yield x::ys  
  4.      | }  
  5. perm: [T](List[T])List[List[T]]  
  6.  
  7. scala> perm(1::2::3::Nil)  
  8. res2: List[List[Int]] = List(List(123), List(132), List(213),  
  9.                              List(231), List(312), List(321))  
  10.  

但是這個(gè)轉(zhuǎn)換規(guī)則還不甚完美。當(dāng)轉(zhuǎn)換后的表達(dá)式含有filter方法的時(shí)候,會(huì)產(chǎn)生幾個(gè)問(wèn)題。

(a)性能問(wèn)題

以一個(gè)簡(jiǎn)單的問(wèn)題為例:求1~999中所有偶數(shù)之和。

下面是兩段類似的解決代碼:

  1. val set = 1 until 1000 
  2. var sum = 0 
  3. for(num <- set;if(num%2 == 0)) sum += num  

或者把if語(yǔ)句移到括號(hào)外面:

  1. val set = 1 until 1000 
  2. var sum = 0 
  3. for(num <- set) if(num%2 == 0) sum += num  

這兩段代碼功能上應(yīng)該是等價(jià)的,但是運(yùn)行效率如何呢?下面首先寫(xiě)一個(gè)粗略的測(cè)試函數(shù):

  1. /**  
  2.   計(jì)算count次調(diào)用call所需的時(shí)間,單位:毫秒  
  3. */ 
  4. def time(call : => Unit,count: Int): Long = {  
  5.     var cnt = count  
  6.     val start = System.currentTimeMillis  
  7.     while(cnt > 0) {  
  8.         call  
  9.         cnt -= 1 
  10.     }  
  11.     System.currentTimeMillis - start  
  12. }  
  13.  

先在Scala2.7.7final中將每段代碼各運(yùn)行十萬(wàn)次:

  1. scala> val set = 1 until 1000 
  2. set: Range = Range(12345678910, ...  
  3.  
  4. scala> time({  
  5.      |     var sum = 0 
  6.      |     for(num <- set;if(num%2 == 0)) sum += num  
  7.      | },100000)  
  8. res3: Long = 47390 
  9.  
  10. scala>  
  11.  
  12. scala> time({  
  13.      |     var sum = 0 
  14.      |     for(num <- set)  if(num%2 == 0) sum += num  
  15.      | },100000)  
  16. res4: Long = 3344 
  17.  
  18. scala>  
  19.  

測(cè)試結(jié)果很出乎意料:兩段類似的代碼,性能竟相差一個(gè)數(shù)量級(jí)!

之所以會(huì)有這么大的差異,根據(jù)上述的轉(zhuǎn)換規(guī)則,第一段代碼將會(huì)轉(zhuǎn)換為下面的實(shí)際執(zhí)行代碼:

  1. val set = 1 until 1000 
  2. var sum = 0 
  3. set.filter{ num => num%2 == 0) }.foreach{ case num => sum += num }  

而第二段代碼實(shí)際執(zhí)行的是:

  1. val set = 1 until 1000 
  2. var sum = 0 
  3. set.foreach{ case num => if(num%2 == 0) sum += num }  

相對(duì)而言,第一段代碼會(huì)調(diào)用filter方法,創(chuàng)建一個(gè)新的集合類,而這個(gè)集合類包含了1~999中所有的偶數(shù)。顯然這個(gè)過(guò)程是比較昂貴的,也是不必要的。

(b)運(yùn)行順序

  1. for (p <- e if g) e'  
  2.  

為例,實(shí)際運(yùn)行的代碼為:

  1. e.filter{ (x1,...,xn) => g }.foreach{ case p => e'}  
  2.  

可以看到,雖然直觀上if g與e'在遍歷的時(shí)候應(yīng)該是依次循環(huán)執(zhí)行;但事實(shí)上轉(zhuǎn)換后if g整體先于e'執(zhí)行。當(dāng)g與e'中同時(shí)包含一個(gè)變量v,并且在g中對(duì)變量v進(jìn)行改動(dòng)時(shí),實(shí)際運(yùn)行結(jié)果可能和我們所預(yù)想的不一致。可能問(wèn)題描述的不是很清楚,下面引用fineqtbull同學(xué)在for語(yǔ)句中內(nèi)嵌if語(yǔ)句的副作用一文中的代碼作為例子來(lái)說(shuō)明:實(shí)現(xiàn)compress方法,其功能是將一個(gè)list中連續(xù)相同的元素刪減至一個(gè)。比如compress(List(1,1,2,3,3,1,1,4)) == List(1,2,3,1,4),下面是兩段類似的實(shí)現(xiàn)代碼,咋一看都沒(méi)問(wèn)題,但運(yùn)行結(jié)果卻不一樣。

  1. Welcome to Scala version 2.7.7.final (Java HotSpot(TM) Client VM, Java 1.6.0_17).  
  2.  
  3. scala> def compress1[T](ls: List[T]): List[T] = {  
  4.      |     var res = List(ls.first)  
  5.      |     for(x <- ls) if(x != res.last) res = res:::List(x)  
  6.      |     res  
  7.      | }  
  8. compress1: [T](List[T])List[T]  
  9.  
  10. scala> def compress2[T](ls: List[T]): List[T] = {  
  11.      |     var res = List(ls.first)  
  12.      |     for(x <- ls;if(x != res.last)) res = res:::List(x)  
  13.      |     res  
  14.      | }  
  15. compress2: [T](List[T])List[T]  
  16.  
  17. scala> compress1(List(1,1,2,3,3,1,1,4))  
  18. res0: List[Int] = List(12314)  
  19.  
  20. scala> compress2(List(1,1,2,3,3,1,1,4))  
  21. res1: List[Int] = List(12334)  
  22.  
  23. scala>  
  24.  

有了之前的說(shuō)明,我們不難發(fā)現(xiàn)其原因所在。但這樣的結(jié)果顯然違反了C/Java中習(xí)慣用法,很容易讓一個(gè)剛接觸Scala的人產(chǎn)生困惑。

2.Scala2.8中的for表達(dá)式

剛才已經(jīng)提到了Scala2.8之前for表達(dá)式所存在的兩個(gè)問(wèn)題,那么在Scala2.8中這兩個(gè)問(wèn)題有沒(méi)有得到解決呢?下面將之前的代碼在2.8.0.r20117-b20091213020323重新運(yùn)行一次試試:

  1. Welcome to Scala version 2.8.0.r20117-b20091213020323 (Java HotSpot(TM) Client VM, Java 1.6.0_17).  
  2.  
  3. scala> def time(call : => Unit,count: Int): Long = {  
  4.      |     var cnt = count  
  5.      |     val start = System.currentTimeMillis  
  6.      |     while(cnt > 0) {  
  7.      |         call  
  8.      |         cnt -= 1 
  9.      |     }  
  10.      |     System.currentTimeMillis - start  
  11.      | }  
  12. time: (call: => Unit,count: Int)Long  
  13.  
  14. scala> val set = 1 until 1000 
  15. set: scala.collection.immutable.Range ...  
  16.  
  17. scala> time({  
  18.      |     var sum = 0 
  19.      |     for(num <- set;if(num%2 == 0)) sum += num  
  20.      | },100000)  
  21. res0: Long = 6906 
  22.  
  23. scala> time({  
  24.      |     var sum = 0 
  25.      |     for(num <- set)  if(num%2 == 0) sum += num  
  26.      | },100000)  
  27. res1: Long = 4312 
  28.  
  29. scala> def compress1[T](ls: List[T]): List[T] = {  
  30.      |     var res = List(ls.first)  
  31.      |     for(x <- ls) if(x != res.last) res = res:::List(x)  
  32.      |     res  
  33.      | }  
  34. compress1: [T](ls: List[T])List[T]  
  35.  
  36. scala> def compress2[T](ls: List[T]): List[T] = {  
  37.      |     var res = List(ls.first)  
  38.      |     for(x <- ls;if(x != res.last)) res = res:::List(x)  
  39.      |     res  
  40.      | }  
  41. compress2: [T](ls: List[T])List[T]  
  42.  
  43. scala> compress1(List(1,1,2,3,3,1,1,4))  
  44. res2: List[Int] = List(12314)  
  45.  
  46. scala> compress2(List(1,1,2,3,3,1,1,4))  
  47. res3: List[Int] = List(12314)  
  48.  
  49. scala>  
  50.  

#T#可以看到Scala2.8中已經(jīng)很好的解決了這兩個(gè)問(wèn)題。對(duì)于一門(mén)語(yǔ)言,要想對(duì)已經(jīng)存在的問(wèn)題進(jìn)行改進(jìn)是很困難的,因?yàn)槭紫冗@些改進(jìn)要盡可能少的影響已經(jīng)存在的舊有代碼,另一方面不能帶來(lái)新的問(wèn)題。幸運(yùn)的是,Martin想到了一個(gè)簡(jiǎn)單而優(yōu)雅的方法成功做到了這些。下面簡(jiǎn)單的敘述一下Martin的解決方法,感興趣的同學(xué)可以去看Martin的原文Rethinking filter。

首先,Martin引入了一個(gè)新的方法withFilter,這個(gè)方法與filter方法一樣以一個(gè)條件函數(shù)A => Boolean作為參數(shù)。與filter方法不同的是,withFilter方法是lazy的。它并不會(huì)創(chuàng)建一個(gè)新的包含符合條件的元素所組成的集合類,而是返回一個(gè)代理類WithFilter,這個(gè)類具有foreach、map、flatMap以及withFilter等方法,并且所有這些方法的調(diào)用是與原來(lái)?xiàng)l件組合的結(jié)果。下面是TraversableLike中WithFilter的實(shí)現(xiàn),Scala2.8中所有的集合類都繼承了TraversableLike:

  1. class WithFilter(p: A => Boolean) {  
  2.  
  3.   /** Builds a new collection by applying a function to all elements of the  
  4.    *  outer $coll containing this `WithFilter` instance that satisfy predicate `p`.  
  5.    *  
  6.    *  @param f      the function to apply to each element.  
  7.    *  @tparam B     the element type of the returned collection.  
  8.    *  @tparam That  $thatinfo  
  9.    *  @param bf     $bfinfo  
  10.    *  @return       a new collection of type `That` resulting from applying the given function  
  11.    *                `f` to each element of the outer $coll that satisfies predicate `p`  
  12.    *                and collecting the results.  
  13.    *  
  14.    *  @usecase def map[B](f: A => B): $Coll[B]   
  15.    *    
  16.    *  @return       a new $coll resulting from applying the given function  
  17.    *                `f` to each element of the outer $coll that satisfies predicate `p`  
  18.    *                and collecting the results.  
  19.    */ 
  20.   def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {  
  21.     val b = bf(repr)  
  22.     for (x <- self)   
  23.       if (p(x)) b += f(x)  
  24.     b.result  
  25.   }  
  26.  
  27.   /** Builds a new collection by applying a function to all elements of the  
  28.    *  outer $coll containing this `WithFilter` instance that satisfy predicate `p` and concatenating the results.   
  29.    *  
  30.    *  @param f      the function to apply to each element.  
  31.    *  @tparam B     the element type of the returned collection.  
  32.    *  @tparam That  $thatinfo  
  33.    *  @param bf     $bfinfo  
  34.    *  @return       a new collection of type `That` resulting from applying the given collection-valued function  
  35.    *                `f` to each element of the outer $coll that satisfies predicate `p` and concatenating the results.  
  36.    *  
  37.    *  @usecase def flatMap[B](f: A => Traversable[B]): $Coll[B]  
  38.    *   
  39.    *  @return       a new $coll resulting from applying the given collection-valued function  
  40.    *                `f` to each element of the outer $coll that satisfies predicate `p` and concatenating the results.  
  41.    */ 
  42.   def flatMap[B, That](f: A => Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {  
  43.     val b = bf(repr)  
  44.     for (x <- self)   
  45.       if (p(x)) b ++= f(x)  
  46.     b.result  
  47.   }  
  48.  
  49.   /** Applies a function `f` to all elements of the outer $coll containing this `WithFilter` instance  
  50.    *  that satisfy predicate `p`.  
  51.    *  
  52.    *  @param  f   the function that is applied for its side-effect to every element.  
  53.    *              The result of function `f` is discarded.  
  54.    *                
  55.    *  @tparam  U  the type parameter describing the result of function `f`.   
  56.    *              This result will always be ignored. Typically `U` is `Unit`,  
  57.    *              but this is not necessary.  
  58.    *  
  59.    *  @usecase def foreach(f: A => Unit): Unit  
  60.    */     
  61.   def foreach[U](f: A => U): Unit =   
  62.     for (x <- self)   
  63.       if (p(x)) f(x)  
  64.  
  65.   /** Further refines the filter for this $coll.  
  66.    *  
  67.    *  @param q   the predicate used to test elements.  
  68.    *  @return    an object of class `WithFilter`, which supports  
  69.    *             `map`, `flatMap`, `foreach`, and `withFilter` operations.  
  70.    *             All these operations apply to those elements of this $coll which  
  71.    *             satify the predicate `q` in addition to the predicate `p`.  
  72.    */ 
  73.   def withFilter(q: A => Boolean): WithFilter =   
  74.     new WithFilter(x => p(x) && q(x))  
  75. }  

在Scala2.8中,for表達(dá)式的轉(zhuǎn)換方式大體保持不變,只是將以前使用filter的地方全部替換為withFilter方法。

結(jié)語(yǔ):可以看到Scala2.8成功解決了之前Scala中存在的一些問(wèn)題,使得Scala語(yǔ)言變得更加優(yōu)雅、強(qiáng)大。你還在等待Java7的閉包嗎?趕緊去嘗試Scala2.8吧^_^

責(zé)任編輯:yangsai 來(lái)源: JavaEye博客
相關(guān)推薦

2009-07-21 14:03:00

Scalaif表達(dá)式while循環(huán)

2009-07-21 14:16:18

Scalafor表達(dá)式

2009-07-21 14:38:08

Scalamatch表達(dá)式break和conti

2012-07-18 09:45:32

Java 8ScalaLambda

2021-05-25 09:18:04

正則表達(dá)式Linux字符串

2009-08-19 14:48:57

正則表達(dá)式性能

2009-08-10 16:57:21

Lambda表達(dá)式

2010-10-21 10:56:29

SQL Server查

2024-03-25 13:46:12

C#Lambda編程

2009-07-21 14:30:38

Scalatry-catch

2018-09-27 15:25:08

正則表達(dá)式前端

2014-01-05 17:41:09

PostgreSQL表達(dá)式

2012-06-26 10:03:58

JavaJava 8lambda

2024-01-04 08:25:03

String表達(dá)式工具

2009-08-07 14:24:31

.NET正則表達(dá)式

2012-03-31 15:09:51

JavaFel

2024-03-13 13:09:14

性能智能座艙軟件

2009-09-16 15:45:56

email的正則表達(dá)式

2022-01-14 07:56:39

C#動(dòng)態(tài)查詢

2021-11-10 09:45:06

Lambda表達(dá)式語(yǔ)言
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)