Functional fever is driving me crazy. I have been trying to write/convert every piece of code to a functional style. For the last couple of weeks, I have been writing tons of code in Scala using pattern matching. In a flight some time back, when I was looking at some C code of calculating the largest number in a given array recursively, I started jotting down Scala equivalent of the same using Pattern Matching. And here’s what I scribbled.
def findMax(lst:List[Int]):Int = { lst match{ case x::Nil => x case x::tail => { if(x < tail(0)) findMax(tail) else findMax(x::tail.tail) } } } println(findMax(List(1111,23,4,5,74,6,578,8,9)))
After the flight, came home and showed the code to my Java developer friend, who looked at it and said it flew over his head. I felt extremely happy about it!!! ;). I also told him that there’s lot of code like that in my Git repo, only to make him run away.