Consider the following code,
1 var ip = ArrayBuffer[String]()
2 ip += "1"
3 println(ip)
4 ip.clear()
5 (1 to 10).foreach(ip += ("1"))
6 println(ip)
Here in line no: 5 using variable ip
inside higher order function results in exception. I do know using var
is not advisable but I want to know how to use vars inside higher order functions. Or is there an alternative to mange state.