2016年7月19日 星期二

[Scala 範例代碼] Scala List/Array/Vector/Seq class filter method examples

Source From Here
Preface
The Scala List class filter method implicitly loops over the List/Seq you supply, tests each element of the List with the function you supply. Your function must return true or false, and filter returns the list elements where your function returns true. Let's look at a few simple examples. In this first example we filter a small list of numbers so that our resulting list only has numbers that are greater than 2:
Note:
Even though I use a List in these examples, the filter method can be used on any Scala sequence, including ArrayArrayBufferVector etc.

scala> val nums = List(5, 1, 4, 3, 2)
nums: List[Int] = List(5, 1, 4, 3, 2)

scala> nums.filter( _ > 2 )
res0: List[Int] = List(5, 4, 3)

Note that in the real world you'd assign the filtered results to a new List, like this:
  1. val originalList = List(51432)  
  2. val newList = originalList.filter(_ > 2)  
This example shows how to get the even numbers from a List using a simple modulus test:
scala> nums.filter( _ % 2 == 0).sorted
res2: List[Int] = List(2, 4)

scala> nums.filter( _ % 2 == 0).sortWith(_ < _)
res3: List[Int] = List(2, 4)

scala> nums.filter( _ % 2 == 0).sortWith(_ > _)
res4: List[Int] = List(4, 2)

filter method examples with a List of Strings
Here are two filter method examples with a list of Strings:
scala> val fruits = List("orange", "peach", "apple", "banana")
fruits: List[String] = List(orange, peach, apple, banana)

scala> fruits.filter(_.length > 5)
res6: List[String] = List(orange, banana)

scala> fruits.filter(_.startsWith("a"))
res7: List[String] = List(apple)


Combining filter, sort, and map
From the excellent book, Beginning Scala, here's a nice combination of the List filtersort, and map methods:
  1. trait Person {  
  2.   def first: String  
  3.   def age: Int  
  4.   def valid: Boolean  
  5. }  
  6.   
  7. // Returns the first name of 'valid' persons, sorted by age  
  8. def validByAge(in: List[Person]) =  
  9.   in.filter(_.valid).  
  10.   sort(_.age < _.age).  
  11.   map(_.first)  
The following example shows how you can use filter with map to transform the type of data that the expression returns. In this case we'll start with a sequence of Person objects, and transform it into a sequence of String objects. We'll start with a simple case class:
scala> case class Person(first:String, last:String, mi:String)
defined class Person

Next, we'll create a little sequence of Person objects:
scala> val fred = Person("Fred", "Flintstone", "J")
fred: Person = Person(Fred,Flintstone,J)

scala> val wilma = Person("Wilma", "Flintstone", "A")
wilma: Person = Person(Wilma,Flintstone,A)

scala> val barney = Person("Barney", "Rubble", "J")
barney: Person = Person(Barney,Rubble,J) 
scala> val betty = Person("Betty", "Rubble", "A")
betty: Person = Person(Betty,Rubble,A)

scala> val peeps = Seq(fred, wilma, barney, betty)
peeps: Seq[Person] = List(Person(Fred,Flintstone,J), Person(Wilma,Flintstone,A), Person(Barney,Rubble,J), Person(Betty,Rubble,A))

Finally, we'll combine filter and map to get a list of all first names where the last name is "Flintstone":
scala> peeps.filter(_.last == "Flintstone").map(_.first)
res8: Seq[String] = List(Fred, Wilma)

I initially wrote this as a for/yield loop, but then realized I could write this much more concisely with this approach. At the moment I find the for/yield loop to be more readable, and this to be much more concise. In my opinion, this code can be made a little more readable by using a variable name in the map expression, as a reminder that you're still dealing with Person objects:
scala> peeps.filter(_.last == "Flintstone").map(person => person.first)
res1: Seq[String] = List(Fred, Wilma)

Scala List filter method summary
I hope these filter method examples have been helpful. Here's a quick summary of how the filter method works:
* filter implicitly loops over a List.
* filter takes a function as an argument. That function should take one List element as input, perform the test you define, and then return either true or false (a Boolean).
* filter only returns List elements that match the filtering expression.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...