Scala 2.8 Arrays are not Traversables

One performance/consistency change that has been make in Scala 2.8 is to make Scala Array always be a Java Array. This has some consequences which we will examine in this post. The biggest one is that Array is not a Scala Collection/Traversable. It is implicitly converted to one but it is not an instance of a Traversable. There are several reasons this was done. Probably the biggest is for performance. Because a Scala array is a Java array there is no overhead when using a Scala array.

Thanks to implicit type conversion all the normal collection methods are useable with an array. Even better, after running a method like map the result will again be a Java array. So the API is much more consistent.

An example illustrating that an Array is not a Traversable:

  1. // This does not compile (which is good) 
  2. // because Traversable[Int] can never be an array
  3. scala> def x(t:Traversable[Int]) = t match {
  4.      | case x : Array[Int] => true          
  5.      | }
  6. < console>:13: error: pattern type is incompatible with expected type;
  7.  found   : Array[Int]
  8.  required: Traversable[Int]
  9.        case x : Array[Int] => true
  10.                 ^
  11. < console>:13: error: type mismatch;
  12.  found   : Array[Int]
  13.  required: Traversable[Int]
  14.        case x : Array[Int] => true
  15.               ^


Another example:

  1. scala> def x(t:Traversable[Int]) = t.isInstanceOf[Array[_]]
  2. x: (t: Traversable[Int])Boolean
  3. /* this evaluates to false because Array is converted
  4.  * to WrappedArray because it has to be implicitly converted
  5.  * to a Traversable.  Since Array is not a Traversable the resulting 
  6.  * object is not an Array
  7.  */
  8. scala> x(Array(1,2,3))                                     
  9. res24: Boolean = false
  10. scala> def x(t:Traversable[Int]) = println(t)
  11. x: (t: Traversable[Int])Unit
  12. // This method call demonstrates the previous assertion
  13. scala> x(Array(1,2,3))                                            
  14. WrappedArray(1, 2, 3)


So suppose you want to be able to accept and use arrays and Traversables in a method but you want to be able to
check that the parameter is an Array. Why not match against WrappedArray. You probably can, but you may get performance improvements in some cases if you don’t require wrapping the array.

For a more concrete example of why you may want to do this. In a Input/Output routine I wrote I would write the data one way if the input was an Array: stream.write(array). But if the input was a traversable then I would have to handle it differently. My particular issue was more complicated than that but that is the basic issue.

So the work around is to define a Generic parameter for the method:

  1. scala> def x[T <% Traversable[Int]](t:T) = t match { 
  2.      | case x : Array[Int] => true                                
  3.      | }
  4. x: [T](t: T)(implicit evidence$1: (T) => Traversable[Int])Boolean
  5. scala> x(Array(1,2,3))                               
  6. res27: Boolean = true

Continua a leggere

Pubblicato in Senza categoria

Next Anodizing Workshop in Chicago

Join us for the next Anodizing Workshop in Chicago from May 11 – 12, 2010.

If you simply just want to know more about anodizing, or why some parts are rejected and others not, or enjoy a great network opportunity with other people who love to talk about anodizing, then join us by clicking here.

Hear what one of the former attendees said about the Anodizing Workshop:

“I have attended Anne Deacon Juhl’s Anodize Workshop and would recommend it without any reservations. Her workshop is well structured and informative along with her excellent communication skills. Anne’s knowledge made the workshop interesting and enabled her to answer all of my questions to my satisfaction.”

Rick Webster, Nelson Nameplate

Perhaps you want to build a new anodizing line, or just want to improve the old, see how a fully automatic line can look after working with me by taking a look to the right column where a short video shows the automatic anodizing line in Denmark.

If you are interested in knowing more about my products, please feel free to contact me or push the consulting button on top of the site.
__________________________________________________ Continua a leggere

Pubblicato in Senza categoria

we are the pil for that!

it’s early in the desert. it’s supposed to be in the 90′s later. big show tonight. man, i’m nervous. went on friday and saw PIL, who were awesome… and then spent the next two hours trying to get home. what a clusterf—! going early today to see sof… Continua a leggere

Pubblicato in Senza categoria

攻擊與防禦思維

看完 Roamer 大大的「駭客想得和你不一樣!」一文,有頗深的感觸。

「為什麼這麼多單位通過了資安認證後,卻還是被黑得很慘。」
首先「通過資安認證」跟「被黑得很慘」沒有太大關係,沒有通過驗證的單位也有沒被黑過的。怎解釋呢?我想得從資安認證的目地說起了。絕大部分資安認證的目的是為了「絕對安全」嗎?是真的為了把「資安做好」嗎?但是,通過認證後卻都異口同聲的說:我們的資安防護很安全。

突然想起馬雲的一句話:「西方人的智慧,是看見別人看不見的東西;東方人的智慧,是看見當作沒看見。」

這邊大概整理一下 Roamer 大大的:

一、「攻擊者積極,防守者消極」
這點是極重要的一環,也是非常貼近現實的。在「我不是教你使詐」一文中就提及了,每年攻方都有大補丸,較於防守方或 IT 人員默默不聞或遮耳閉眼,兩相比拼,一漲一消,高下立判。
況且,消極攻擊者只要學會一、兩招,就能吃遍天下了,掛馬的還是玩掛馬、DDoS的還是慣用DDoS、偷資料的還是默默協助異地備援、惡意郵件攻擊的仍舊發著釣魚郵件、竄改網頁的仍舊樂此不彼地爭奪排名。
對於防守者來說,就得承受玲瑯滿目、五花八門的招數,充斥了各種威脅與損失。
消極的攻擊,就要積極的防守。攻擊者若積極,哪防守的該如何呢?

二、「木桶理論
這是個很爛的理論,坦白說我不太喜歡,且它會讓人有誤解。或許「木桶比喻」、「木桶概念」比較恰當。因為用「理論」是必須很嚴謹的,木桶的容器根本是不可能把水保護好的,水還會蒸發掉,木片有毛細孔,即便木板密封,水還是會不見,當把水放到木桶中保護,根本就是個錯誤,也讓許多人對其有誤解。做資安第一件事就是要慎選容器,否則,疊床架屋的結果會很慘,再多的緩和、補救、強化、防禦措施都沒有用的。聽說,現在很多人還希望大家把水放到雲端裡,當然不是不行,但是不是所有的水都要放到雲端呢?慎思。

三、「風險的變遷」
這跟第一點是大致相同的,正因為不積極,當然也就更不會注意風險的變化,還有的甚至存在防火牆無敵論、入侵防護神化論,直到被打到痛處。
外在風險變化,通常也是防禦調整重點,如何掌握花三分力氣,得到七分功效,在於你的敏感與即時防禦調整。
另外,區分不同輕重緩急等級的安全區域也是很重要的一個防禦觀念。把七分的力氣花在重點區域上,來求得九分九的安全性,這是快速有效率的一個方式。上軌道後再慢慢增強防護力道。
重點防禦絕對不是資安速成,否則,一不小心留下其他路或通道,哪,後果不堪設想。

四、「商業邏輯問題」
這類問題主要是「人」疏忽造成的問題,也得靠「人」來發現問題。所以主要的問題就是「為何有人會造成這問題」、「誰會利用這問題」,從這兩方面著手通常可以獲得解決。
但是,要徹底解決確不太容易,像是一些交易平台(B2C、C2C)衍生的詐騙問題,光靠資料流的層面是無法解決的,還需要金流、物流方面的配合,但這已經橫跨好幾家公司了。或許,還得要法令的配合才行。

五、「未知弱點」

這點,不可否認是很艱困的一點。重點在於「未知弱點揭漏」的時刻,相關單位是否有足夠的敏感度能夠知悉,或收到一手訊息。有時即使知道問題,也無法予以關閉系統,因為有營運或其他考量,畢竟 IT 往往是支援單位,IT 也只能@*%#&。
關閉系統或停用該功能,通常可以達到有效的因應。否則,IT 往往也只能先施以緩和、修補措施,設法最終再完成強化、防禦作業了。

這裡再殘酷的補上兩腳:

六、「資安打手」
有些單位的資安工作從來不是應付外來攻擊者的,是用來對付內部異己人士、砸自己人腳、凸顯自我官威的…資安亂象,族繁不及備載。或許前面一句話寫得太重,應該說七分對內三分對外吧。政治角力介入凌駕專業之上,這邊就不敘述太多,打開電視就一堆了。

七、「有發生啥事嗎」
我不認為都是以上的原因,通常一再被黑跟這篇文講得差不多。甚至,根本也沒探究問題的成因,只把問題的表象做到復原工作;外面可能全世界都知道他們家出事了,可是他們家可能無人知悉,或只有該系統管理者知道;系統管理者怕究責就自我進行復原,但,根本問題仍然沒解決。最後,發生啥事了嗎?

原來,這才是最高智慧。 Continua a leggere

Pubblicato in Senza categoria

listamania redux

hello from sunny indian wells california. got here really late last night… too late! it’s coachella weekend and we are in some crazy desert resort. i’m looking out on a golf course… it’s gonna be hard to resist. it looks like the temps will be in t… Continua a leggere

Pubblicato in Senza categoria

Daily scala 2010-04-16 08:56:00

A quick note. ScalaDays Rocks! Wish you were here :)

This topic just demonstrates a cute little trick that can occasionally be quite useful:

  1. scala> List(1,2,3) ++ Some(4)
  2. res0: List[Int] = List(1, 2, 3, 4)
  3. scala> List(1,2,3) ++ None   
  4. res1: List[Int] = List(1, 2, 3)


Options are implicitly converted to Iterables, so Options can be appended to collections.

  1. scala> val x : Iterable[Int] = None       
  2. x: Iterable[Int] = List()
  3. scala> val x : Iterable[Int] = Some(4)
  4. x: Iterable[Int] = List(4)

Continua a leggere

Pubblicato in Senza categoria

new gold soundz!

this is the japanese box set. out through hostess records in japan. not sure how to get a hold of it yet, but matador will probably have some available soon.it’s all the studio records plus the videos and a couple live shows on dvd. all the cds are in … Continua a leggere

Pubblicato in Senza categoria

we want two two states!

hello from the city of angels. yes… we’ve heard ‘under the bridge’ on the radio like 5 times already. it’s good to be back in california! no star sightings yet, but everyone is one here right! had some beautiful oaxacan food last night in little kore… Continua a leggere

Pubblicato in Senza categoria