What is the equivalent version of this block in java

I came across the following block in Scala . What is the equivalent version in java ( both with lambda , without lambdas - inline functions )

def valueAtOneQuarter(f: (Double) => Double) = f(0.25)

What don’t you understand about it? It think the equivalent java code is something like this

public Double valueAtOneQuarter(Function1<Double, Double> f) {
    return f.apply(0.25);
}
2 Likes

Ok, I need to explore what Function1 means - I guess it is in built function abstraction in Scala ?
hmm. apply - magic function - does many things .

And also, from Scala impatient guide, the type for the above function is mentioned as

((Double) => Double) => Double

However, when i executed the above like in REPL, I got the following

valueAtOneQuarter: (f: Double => Double)Double

So , it seems both syntaxes are similar ?

((Double) => Double) => Double

This is technically a lie.
The book probably says that you can think of valueAtOneQuarter as a higher-order function; meaning a function that receives another function. So its type would be the one that you mentioned.

However, a def creates a method not a function, those two are very different; especially in Scala 2, Scala 3 will reduce drastically the number of differences.
However, the main difference (which won’t change) is that functions are values whereas methods are not; that is why the REPL displays it differently.

While it is important to know that methods and functions are different, and understand their differences.
You usually can usually forget about that, especially because eta-expansion allows to quickly convert a method into a function.

I guess it is in built function abstraction in Scala

Yes, Scala has built-in functions, both as values as well as literals for creating those values.

apply - magic function - does many things.

Not sure what you mean with this.

1 Like

If they are not familiar with Lambdas in Java I quite understand that they don’t get what the Scala notation does.

Well they asked for java and mentioned lambdas specifically so I guess they know what it is.

Also, it wasn’t meant as a rhetorical question. Apologies if it came across as such.

2 Likes