Looking at the Scala standart library I 've noticed that many of the functions(methods) are written in imperative style : loops are used more often than tailrec and so on. For example:
@inline final override def takeWhile(p: A => Boolean): List[A] = {
val b = new ListBuffer[A]
var these = this
while (!these.isEmpty && p(these.head)) {
b += these.head
these = these.tail
}
b.toList
}
Can be easily rewritten(as function):
def takeWhile[A](p:A=>Boolean)(l:List[A]): List[A] ={
@tailrec
def takeWh(p:A=>Boolean,l:List[A],r:List[A]):List[A]={
if(l.isEmpty) r
else if(p(l.head)) takeWh(p,l.tail,l.head::r)
else r
}
takeWh(p,l,Nil).reverse
}
Why Scala developers prefer imperative style over functional if Scala is supposed to be a functional language?