Skip to content

Instantly share code, notes, and snippets.

@sidharthkuruvila
sidharthkuruvila / gist:3154845
Created July 21, 2012 06:30
Utility to functions to convert between camel case and underscore separated names
/**
* Takes a camel cased identifier name and returns an underscore separated
* name
*
* Example:
* camelToUnderscores("thisIsA1Test") == "this_is_a_1_test"
*/
def camelToUnderscores(name: String) = "[A-Z\\d]".r.replaceAllIn(name, {m =>
"_" + m.group(0).toLowerCase()
})
@ShaneDelmore
ShaneDelmore / gist:a4a6560e5952b20d2e33
Created September 17, 2014 20:28
sum and max in scala, tail recursive without an inner helper method
//If we have a list with at least two elements sum them and add them to the rest of the list
//else return the head of the list
@tailrec
def sum(xs: List[Int]): Int = xs match {
case h :: i :: rest => sum(h + i :: rest) //we have a list with at least two elements
case _ => xs.head
}
//If we have a list with at least two elements, remove the smallest and recurse
//else return the head of the list/We have