I'm starting out with scala, and trying to apply the functional way to it, but I came out with bunch of nested ifelse constructions which is hard to read, and I wonder is there nicer way to program such things?
For example I wrote a script, that performs parentheses balancing:
def balance(chars: List[Char]): Boolean = {
def checkParentesys(chars: List[Char], parentesis: List[Char]): Boolean =
if (chars.isEmpty && parentesis.isEmpty)
true
else
if (chars.head == '(')
checkParentesys(chars.tail, '(' :: parentesis)
else
if (parentesis.isEmpty)
false
else
checkParentesys(chars.tail, parentesis.tail)
checkParentesys(chars.filter(s => s == '(' || s == ')'), List())
}
How can I write it to be more functional and more scala like?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…