scala - what's the difference between {} and () when using filter? -
To filter a list using the function of the filter, if it uses You are not using a simple expression. Your first form with You can leave edit Even less: These are simple expressions that can be used as arguments for {} , But if it is used
() then it has failed, but why?
scala & gt; Sea Race 25: List [(Int, Int)] = List ((1.0), (3,2), (5,4)) Scale & gt; D res26: list [(int, int)] = list ((1,0), (3,2), (5,4)) scale & gt; Val d = b.philter {case (x, y) = & gt; Y% 2 == 0} d: list [(int, int)] = list ((1,0), (3,2), (5,4)) scale & gt; Val d = b.philter (case (x, y) => y% 2 == 0) & lt; Console & gt ;: 1: Error: Valid start of valid expression val d = b.filter (case (x, y) = & gt; y% 2 == 0) ^
{}
scala> Is a shortcut for D.filter ({case (x, y) => y% 2 == 0}) res177: list [(int, int)] = list ((1,0), (3, 2), (5, 4))
() but not
{} . It is not necessary to use
case . You can see the
scala.Tuple2 :
scala> Can reach. D.filter (t = & gt; t._2% 2 == 0) res182: list [(int, int)] = list ((1,0), (3,2), (5,4)) < / Code>
scala> D.filter (_._ 2% 2 == 0) res183: list [(int, int)] = list ((1,0), (3,2), (5,4)) < / Pre>
filter .
Comments
Post a Comment