When you have a function that wraps another one in Scala. What do you call the nested function?
I found this :
def factorial(i: Int): Int = {
def fact(i: Int, accumulator: Int): Int = {
if (i <= 1)
accumulator
else
fact(i - 1, i * accumulator)
}
fact(i, 1)
}
I usually use just do because it is shorter, generic, and explicit. But is there a better way or a naming convention for nested functions?
def myFunction() = {
def do(i: Int) = {
...
}
do(1)
}
4
There is no need for a convention. Local functions are not exposed to the rest of the code, so nothing anybody else writes will care about it. I say choose something that makes sense in context, rather than worry about a convention which may work well in one place, not in another.
Here is a tail-recursive fibonacci implementation:
def fib (n: Long): Long = {
def loop(current: Long, next: => Long, iteration: Long): Long = {
if (n == iteration)
current
else
loop(next, current + next, iteration + 1)
}
loop(0, 1, 0)
}
I could have called it fibloop, but I don’t really think that adds anything. It’s not accessible anywhere else, so why bother “namespacing” it? Perhaps calling it forwardloop might be clearer, but not fibloop. If anything, calling it loop emphasises that it is local.
If your methods/functions are so long that you’ve forgotten where a helper/local function was defined by the time you see it again, I could see the need for a naming convention (although if the function is named so as to make its purpose clear, how often will it matter?). Personally, I try to avoid writing anything that verbose, but if you do, then I suggest you save special conventions for those methods where this actually becomes a problem. Even then, I bet a well chosen name will be more helpful than any number of underscores.
If a method is succinct enough, (and the local function also simple) I confess I sometimes use a function literal just to avoid having to worry about the name.
Now, you might counter that prefixing local functions with an underscore definitively avoids the possibility of shadowing an external function. To which I would respond by asking what happens if you have a local function inside another local function? Use two underscores? If you re-organise your code, how much search-and-replace are you prepared to do, to chase after this problem you created for yourself?
A convention I’ve seen in some other languages is to use a leading or trailing underscore for a private, temporary named function that exists purely as an implementation detail:
def factorial(i : Int): Int = {
def _factorial(i : Int, acc : Int): Int = {
...
}
_factorial(i)
}
I don’t believe there’s an official standard in this case. Just calling it fact
or factorialImplementation
or something should be fine.
Speaking of factorial, I assume this is being done for the sake of learning right? Because there are a number of shorter and potentially easier-to-maintain ways to get the same results. I like the implementation that uses reduceLeft
(or foldl1
if you come from Haskell-land):
Stream.from(1).take(n).reduceLeft(_*_)
2