2015年6月7日 星期日

scala list collect 用法

scala list collect 用法


collect methoad 接受 PartialFunction 作為參數。結果產生一個 list。輸出 list 的內容是該 PartialFunction 有定義且由 PartialFunction 運算後的值

PartialFunction 長的像這樣

{
    case a: Int => a.toString
    case a: String => 1
}


以下面例子作為說明

List(1, 2, "3", Nil) collect { case a: Int => a.toString case b: String => 1}

其結果為

List(1, 2, 1)

說明

原始的 list 內容是整數 1, 2
和字串 3 和一個 Nil 物件

整數 1 和 2 有定義在 PartialFunction,
因此1 和 2 經過 PartialFunction 運算後,會放在結果

又字串 3 有定義在 PartialFunction,
因此 3 經過 PartialFunction 運算後,會放在結果

又 Nil 不沒有定義在 PartialFunction
因此沒有出現在結果

1 則留言:

  1. Upon her launch, Sun visited several of} Las Vegas casino reward retailers and purchased memento decks of taking part in} cards. They look similar to those used on the gaming tables but have holes punched through their centers to forestall cheaters from slipping a memento ace of spades, say, into a poker game. Some had crisscrossing patterns that went proper to all four edges. The patterns on these cards, as a consequence of the manufacturing processes, had been trimmed barely differently way|in another way} on top and bottom, resulting in uneven margins of 1/32 of an inch or much less. She spent around a thousand hours, over four years, coaching herself to acknowledge the minute variations on particular cards. Sun figured out how she could leverage these differences that had been nearly imperceptible and 메리트카지노 acceptable by trade standards.

    回覆刪除