scala - what's the difference between {} and () when using filter? -


To filter a list using the function of the filter, if it uses {} , 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) ^    

You are not using a simple expression. Your first form with {}

  scala> Is a shortcut for D.filter ({case (x, y) => y% 2 == 0}) res177: list [(int, int)] = list ((1,0), (3, 2), (5, 4))   

You can leave () but not {} . It is not necessary to use

edit

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>  

Even less:

  scala> D.filter (_._ 2% 2 == 0) res183: list [(int, int)] = list ((1,0), (3,2), (5,4))  < / Pre> 

These are simple expressions that can be used as arguments for filter .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -