2

I am trying to make sense of a custom Iterator in Scala written by another programmer. I am having trouble understanding the function declarations. They look like anonymous functions to me, but I simply can't wrap my head around them fully.

I did some reading about Anonymous Functions in Scala , and I found this resource [http://www.scala-lang.org/old/node/133] helpful, but I still cannot read the above functions and make sense of them completely.

Here is the code:

class MyCustomIterator(somePath: Path, someInt: Int, aMaxNumber: Int) {
      def customFilter:(Path) => Boolean = (p) => true
       // Path is from java.nio.files.Path
      def doSomethingWithPath:(Path) => Path = (p) => p
}

I would like to understand these understand these functions. What is the return type really? What is the body of the function?

.

1 Answer 1

5

(For the first def) The parts after the colon and before the equals sign are the return type. So, the return type is:

Path => Boolean

Which denotes a function signature.

Now, breaking that down, the item on the left of the arrow is the parameters of a function. The right hand side is the return type of the function.

So, it is returning a function that accepts a Path and returns a Boolean. In this case, it is returning a function that will accept a Path and return true no matter what.

The second def returns a function that accepts a Path and returns another Path (the same Path in this case)

An example usage would be to use them as follows:

First method:

iter.customFilter(myPath) //returns true

or

val pathFunction = iter.customFilter;
pathFunction(myPath) //returns true

Second method:

iter.doSomethingWithPath(myPath) //returns myPath

or

val pathFunction = iter.doSomethingWithPath
pathFunction(myPath) //returns myPath
Sign up to request clarification or add additional context in comments.

4 Comments

Could add, more usual to see def customFilter: Path => Boolean = _ => true and def doSomethingWithPath: Path => Path = identity. Is that easier to read?
Justin, I like your explanation. However, let me get my understanding fully right: "The item on the left of the arrow is the parameters of a function. " - is it "Boolean = (p) => true" If that is so, how do we interpret this syntax.
@som-snytt, that is easier to read. Path is the return type, and this thing "Boolean = _ => true" is the return type? What is the underscore _ ? It is like a placeholder, I feel. And what did you mean by: Path = identity I can't get the Identity part.
@user3825558, I think something like this is clearer: def customFilter: (Path => Boolean) = (_ => true) or def customFilter: (Path => Boolean) = ((p) => true). These are exactly identical to the ones in the original code. Now the parentheses clearly delimit types and expressions, and you can see that customFilter is a method returning a function.

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.