How do you make this method work?
def filled(b: Board): Board = {
b.next foreach { n =>
if (n.filled) n
else filled(n)
}
}
I need it to return after first filled n (n.filled == true) like in Java.
Now I get:
chess-knight.scala:72: error: type mismatch;
found : Unit
required: this.Board
b.next foreach { n =>
^
one error found
Thanks!
Isn't this what you need?
def filled(b: Board): Board = b.next find {_.filled} get
Providing that b.next
is a Seq[Board]
and there is always at least one filled
Board
.
If you insist on purely functional prefer an approach exploiting pattern matching over List
:
def filled(b: Board): Board = {
b.next match {
case n :: _ if(n.filled) => n
case _ :: rest => filled(rest)
case Nil => throw NoSuchElementException
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.