Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala map method syntax

Tags:

syntax

scala

The code below from http://www.scalaclass.com/book/export/html/1 to do matrix dot product.

I can't understand the syntax between the curly brackets.

  • Why are the curly brackets used, not the regular method parentheses?
  • Is t an anonymous method?
  • What is ._1 and ._2?

Thanks.

type Row    = List[Double] type Matrix = List[Row]  def dotProd(v1:Row, v2:Row) =      v1.zip(v2).map{ t:(Double, Double) => t._1 * t._2 }.reduceLeft(_ + _) 
like image 203
Nabegh Avatar asked May 03 '12 11:05

Nabegh


People also ask

How do I make a map in scala?

There are two kinds of Maps, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed. By default, Scala uses the immutable Map. If you want to use the mutable Map, you'll have to import scala.

How do I access scala map?

Scala Map get() method with exampleThe get() method is utilized to give the value associated with the keys of the map. The values are returned here as an Option i.e, either in form of Some or None. Return Type: It returns the keys corresponding to the values given in the method as argument.

What does => mean in scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .


2 Answers

  • Why are the curly brackets used, not the regular method parentheses?

Some people prefer to use curly braces when the parameter is an anonymous function. For one thing, curly braces enable pattern matching anonymous functions, whereas parenthesis do not. In this particular example, there's no need for curly braces.

Here's an example where curly braces are required (because of the case pattern matching):

def dotProd(v1:Row, v2:Row) =      v1.zip(v2).map{ case (a, b) => a * b }.reduceLeft(_ + _) 

Note that the above function accomplishes the same thing as the one in the question, in a slightly different way.

  • Is t an anonymous method?

No, it is a parameter. Just like v1 and v2 are parameters for dotProd, t is a parameter for the anonymous function being passed to map.

  • What is ._1 and ._2?

Methods on t. The parameter t was defined as being a tuple (specifically, Tuple2[Double, Double], which can be written as (Double, Double)), and tuples let you extract each member of the tuple with methods like that: _1, _2, _3, etc.

A Tuple2 only has _1 and _2, of course. Note that the first parameter is _1, not _0, because of influence from other functional languages.

Anyway, the zip method will convert Row (List[Double]) into a List[(Double, Double)]. The method map takes a function that converts the elements of the list (which are (Double, Double) tuples) into something else.

like image 145
Daniel C. Sobral Avatar answered Sep 24 '22 17:09

Daniel C. Sobral


In this particular case curly brackets have no advantage over plain old syntax, but in general the sweet thing about using curly brackets is that they allow you to write pattern matching expressions inside map ...:

so I can rewrite this

.map{ t:(Double, Double) => t._1 * t._2 } 

into this

.map{ case(a: Double, b: Double) => a*b } 

but this will not compile:

.map( case(a: Double, b: Double) => a*b ) 

._1, ._2 provides access to first, second, ... N element of N-tuple, as Lee said.

like image 41
om-nom-nom Avatar answered Sep 23 '22 17:09

om-nom-nom