Anonymous function declaration

51 views
Skip to first unread message

Olek

unread,
Jun 30, 2016, 5:25:20 PM6/30/16
to scala-user
Hi!

This is my first post. In few weeks I'm going to learn Scala.
I bump into first confusion. I have such declaration.


val test3 = (str: String) : String => {

str + str

}


Why it is not possible?
Why I can't specify the return type as it is in class method?

The


val test3 = (str: String) => {

str + str

}


Works.

Thanks in advance!
Olek

Olek

unread,
Jun 30, 2016, 5:30:47 PM6/30/16
to scala-user
Another confusion. It was said that ( ) are optional for function call with 1-arity

Here is an example:

  1. names.mkString(",")
  2. // is the same as
  3. names mkstrign ","

But when I call 



def main( args : Array[String]) : Unit = {




val test3 = (str: String) => {

    (str + str)

}

println test3 ("wow")
}

The compiler 2.11.8 gives me an error 

Serge

unread,
Jun 30, 2016, 5:32:30 PM6/30/16
to scala...@googlegroups.com
val testFn: String => String = s => s"${s}${s}"
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rex Kerr

unread,
Jun 30, 2016, 5:35:49 PM6/30/16
to Olek, scala-user
These kinds of questions are better asked on the Gitter channel: https://gitter.im/scala/scala

(FWIW, parens can be omitted only when there is an object-method-arg pattern, not just method-arg.)

  --Rex


--

Olek

unread,
Jun 30, 2016, 6:19:24 PM6/30/16
to scala-user
Ok. Thank you for clarifying.
It works as you said. However I look at this in terms of exception.
I have to signature the reference to which I assign the function. But the function itself _can_ be partially annotated with a type on arguments but not on return type.

On Thursday, 30 June 2016 23:25:20 UTC+2, Olek wrote:

Rex Kerr

unread,
Jun 30, 2016, 6:32:18 PM6/30/16
to Olek, scala-user
The return type of a function comes after a `=>`.  `(a: A): B` is not the type of a function.  (It is the signature of a method, without the method name, but methods and functions do not have identical syntax.)  You can annotate the return type, which is what I did in my second example.  You just can't stick the return type onto the list of arguments as if the function were a method with no name.

  --Rex


--

Olek

unread,
Jun 30, 2016, 7:28:17 PM6/30/16
to scala-user, aleksand...@gmail.com
Ok. Now another quiz question:

object MapReduce {


def mapreduce( mapf : ( String, String ) => List[(String, String)] ,
reducef : (String , List[String]) => List[String] ,
coll : List[( String , String)]) : List[(String, String)] = {


coll.map( (x, y )=> mapf(x, y) )



}


}


What is wrong with that?! How to annotate that? Why scala can't infer the return type of map by fact that this is a last statement and function has declared return type?

Rex Kerr

unread,
Jun 30, 2016, 9:33:03 PM6/30/16
to Olek, scala-user
Scala doesn't autotuple function arguments.  It would work if you had mapf: ((String, String)) => List[(String, String)] and then just did coll.map(mapf), or if you unpacked the list tuple, e.g. by coll.map{ case (x,y) => mapf(x,y) }.

(List[(String, String)] is shorthand for List[Tuple2[String, String]].)

It's a bit of a wrinkle in the language design, but since the JVM doesn't unify tuples and argument lists, and to box/unbox it for you can be expensive, Scala makes you pay attention to it.

  --Rex

Olek

unread,
Jul 1, 2016, 5:16:20 PM7/1/16
to scala-user, aleksand...@gmail.com
Thank you for help.
Here is my final implementation https://github.com/naleksander/scalduce
There is also a Clojure version https://github.com/naleksander/mapreduce just for comparison
Reply all
Reply to author
Forward
0 new messages