0

I'm having hard time understanding what the following means in scala:

f: Int => Int

Is the a function? What is the difference between f: Int => Intand def f(Int => Int)?

Thanks

3
  • 2
    def f(Int => Int) isn't valid Scala. Do you mean def f(i: Int): Int? Commented Oct 23, 2014 at 4:20
  • Oh okay. I actually know what def f(i:Int):Int is. So what is f: Int => Int? Commented Oct 23, 2014 at 4:27
  • Try def f(c:Int => Int) = c(_) which would allow you to f(_ * 2)(2) a function that takes a function and produces a function. Interesting when you check the various type signatures. Commented Oct 23, 2014 at 7:15

3 Answers 3

2

Assuming f: Int => Int is a typo of val f: Int => Int,
and def f(Int => Int) is a typo of def f(i: Int): Int.

val f: Int => Int means that a value f is Function1[Int, Int].

First, A => B equals to =>[A, B].
This is just a shortcut writing, for example:

trait Foo[A, B]
val foo: Int Foo String // = Foo[Int, String]

Second, =>[A, B] equals to Function1[A, B].
This is called "type alias", defined like:

type ~>[A, B] = Foo[A, B]
val foo: Int ~> String // = Foo[Int, String]

def f(i: Int): Int is a method, not a function.
But a value g is Function1[Int, Int] where val g = f _ is defined.

Sign up to request clarification or add additional context in comments.

1 Comment

The point about methods and functions is valid, but there are some noteworthy stuff here. Due to eta-reduction in lambda calculus a method value (f in this case) can be treated as a function value (g in this case) in many cases (where compiler can infer that it needs a function type but instead it is given a method value, sometime due to overloading it's not possible and you need to partially apply your method value with _ to explicitly tell compiler that you mean it). So I guess it deserves to be mentioned that f can be used in many places interchangeably with g.
1
f: Int => Int

means that type of f is Int => Int.

Now what does that mean? It means that f is a function which gets an Int and returns an Int.

You can define such a function with

def f(i: Int): Int = i * 2

or with

 def f: Int => Int = i => i * 2

or even with

def f: Int => Int = _ * 2

_ is a placeholder used for designating the argument. In this case the type of the parameter is already defined in Int => Int so compiler knows what is the type of this _.

The following is again equivalent to above definitions:

def f = (i:Int) => i * 2

In all cases type of f is Int => Int.

=>

So what is this arrow? If you see this arrow in type position (i.e. where a type is needed) it designates a function.

for example in

val func: String => String

But if you see this arrow in an anonymous function it separates the parameters from body of the function. For example in

i => i * 2

1 Comment

...or val f = (_: Int) * 2 :)
0

To elaborate just slightly on Nader's answer, f: Int => Int will frequently appear in a parameter list for a high order function, so

def myHighOrderFunction(f : Int => Int) : String = {
   // ...
   "Hi"
}

is a dumb high order function, but shows how you say that myOrderFunction takes as a parameter, f, which is a function that maps an int to an int.

So I might legally call it like this for example:

myHighOrderFunction(_ * 2)

A much more illustrative example comes from Odersky's Programming Scala:

object FileMatcher {
    private def filesHere = (new java.io.File(".")).listFiles
    private def filesMatching(matcher: String => Boolean) =
        for (file <- filesHere if matcher(file.getName))
            yield file
    def filesEnding(query: String) = filesMatching(_.endsWith(query))
    def filesContaining(query: String) = filesMatching(_.contains(query))
    def filesRegex(query: String) = filesMatching(_.matches(query))
}

Here filesMatching is a high order function, and we define three other functions that call it passing in different anonymous functions to do different kinds of matching.

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.