In Haskell, when I need a quick worker function or helper value, I usually use prime ('
) that is widely used in mathematics. For instance, if I were to write a reverse
function and needed a tail-recursive worker, I would name it reverse'
.
In Scala, function names can't contain a '
. Is there any commonly accepted naming scheme for helper functions and values in Scala?
Functions should be named using lowercase, and words should be separated with an underscore. Functions should in addition have the grouping/module name as a prefix, to avoid name collisions between modules.
Why don't you declare the method inside of the method that uses it? Then you can just call it "helper" or whatever you like without having to worry about name conflicts.
scala> def reverse[A](l:List[A]) = {
| def helper(acc:List[A],rest:List[A]):List[A] = rest match {
| case Nil => acc
| case x::xs => helper(x::acc, xs)
| }
| helper(Nil, l)
| }
reverse: [A](l: List[A])List[A]
scala> reverse(1::2::3::Nil)
res0: List[Int] = List(3, 2, 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With