
上QQ阅读APP看书,第一时间看更新
FRP – the filter() function Marble diagram
In FP, a filter() function is used to filter a data stream (or a set of elements—containers) with a condition (this condition is known as a predicate). For instance, we have a list of numbers and want to filter and return only even numbers, as demonstrated here:
scala> val numList = List(1,2,3,4,5,6) numList: List[Int] = List(1, 2, 3, 4, 5, 6) scala> numList.filter(x => x%2 == 0) res8: List[Int] = List(2, 4, 6)
We can represent this filter() function as shown here:
