I have a loan pattern that applies a function n times where ‘i’ is the incrementing variable. “Occasionally”, I want the function passed in to have access to ‘i’….but I don’t want to require all functions passed in to require defining a param to accept ‘i’. Example below…
def withLoaner = (n:Int) => (op:(Int) => String) => {
val result = for(i <- 1 to n) yield op(i)
result.mkString("\n")
}
def bob = (x:Int) => "bob" // don't need access to i. is there a way use () => "bob" instead?
def nums = (x:Int) => x.toString // needs access to i, define i as an input param
println(withLoaner(3)(bob))
println(withLoaner(3)(nums))
(not sure how it is related to the loan pattern)
Edit Little explanation as requested in comment.
Not sure what you know and don’t know of scala and what you don’t undestand in that code. so sorry if what I just belabor the obvious.
First, a scala program consist of traits/classes (also singleton object) and methods. Everything that is done is done by methods (leaving constructor aside). Functions (as opposed to methods) are instances of (subtypes of) the various FunctionN traits (N the number of arguments). Each of them has as apply method that is the actual implemention.
If you write
it is desugared to
(defines an anonymous class extending
Function1, with given apply method and creating an instance)So writing a function has rather more weight than a simple method. Also you cannot have overloading (several methods with the same name, differing by the signature, just what I did above), nor use named arguments, or default value for arguments.
On the other hand, functions are first classes values (they can be passed as arguments, returned as result) while methods are not. They are automatically converted to functions when needed, however there may be some edges cases when doing that. If a method is intended solely to be used as a function value, rather than called as a method, it might be better to write a function.
A function
f, with itsapplymethod, is called withf(x)rather thanf.apply(x)(which works too), because scala desugars function call notation on a value (value followed by parentheses and 0 or more args) to a call to methodapply.f(x)is syntactic sugar forf.apply(x). This works whatever the type off, it does not need to be one of theFunctionN.What is done in withLoaner is returning an object (of an anonymous type, but one could have defined a class separately and returned an instance of it). The object has two
applymethods, one accepting anInt => String, the other one an() => String. When you dowithLoaner(n)(f)it meanswithLoaner(n).apply(f). The appropriate apply method is selected, iffhas the proper type for one of them, otherwise, compile error.Just in case you wonder
withLoaner(n)does not meanwithLoaner.apply(n)(or it would never stop, that could just as well meanwithLoaner.apply.apply(n)), as withLoaner is a method, not a value.