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:
Note that in the real world you'd assign the filtered results to a new List, like this:
This example shows how to get the even numbers from a
List using a simple modulus test:
filter method examples with a List of Strings
Here are two filter method examples with a list of Strings:
Combining filter, sort, and map
From the excellent book, Beginning Scala, here's a nice combination of the List filter, sort, and map methods:
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:
Next, we'll create a little sequence of Person objects:
Finally, we'll combine filter and map to get a list of all first names where the last name is "Flintstone":
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 List filter method summary
I hope these filter method examples have been helpful. Here's a quick summary of how the filter method works:
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:
Note that in the real world you'd assign the filtered results to a new List, like this:
- val originalList = List(5, 1, 4, 3, 2)
- val newList = originalList.filter(_ > 2)
filter method examples with a List of Strings
Here are two filter method examples with a list of Strings:
Combining filter, sort, and map
From the excellent book, Beginning Scala, here's a nice combination of the List filter, sort, and map methods:
- trait Person {
- def first: String
- def age: Int
- def valid: Boolean
- }
- // Returns the first name of 'valid' persons, sorted by age
- def validByAge(in: List[Person]) =
- in.filter(_.valid).
- sort(_.age < _.age).
- map(_.first)
Next, we'll create a little sequence of Person objects:
Finally, we'll combine filter and map to get a list of all first names where the last name is "Flintstone":
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 List filter method summary
I hope these filter method examples have been helpful. Here's a quick summary of how the filter method works:
沒有留言:
張貼留言