Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming scheme for helper functions in Scala

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?

like image 882
mrueg Avatar asked Sep 12 '11 11:09

mrueg


People also ask

How do you name a helper function?

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.


1 Answers

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)
like image 93
Kim Stebel Avatar answered Oct 03 '22 20:10

Kim Stebel