In my project, we are migrating from Java to Scala. I have to a function in Java that uses continue in for loop and returns a value based on an if-condition inside the for loop as below.
private TSourceToken getBeforeToken(TSourceToken token) {
  TSourceTokenList tokens = token.container;
  int index = token.posinlist;     
  for ( int i = index - 1; i >= 0; i-- ) {
    TSourceToken currentToken = tokens.get( i );
    if ( currentToken.toString( ).trim( ).length( ) == 0 ) {
      continue;
    }
    else {
      return currentToken;
    }
  }
  return token;
}
To convert this to Scala, I used Yield option in order to filter out the values that satisfy the else condition of the if expression as below.
def getBeforeToken(token: TSourceToken): TSourceToken = {
  val tokens = token.container
  val index = token.posinlist
  for { i <- index -1 to 0 by -1
        currentToken = tokens.get(i)
        if(currentToken.toString.trim.length != 0)
        } yield currentToken
}
I am not able to figure out how to return the value yielded from the for-loop. The error message is
type mismatch;
Found   : IndexedSeq[TSourceToken]
Required: TSourceToken
I understand that yield collecting all the values from the for-loop and its condition inside and is resulting in a collection: IndexedSeq & hence the error message. I am not able to think of a logic to form as it was written in the Java code.
Could anyone let me know how can I frame the code in Scala without using yield and break the for-loop once the else condition is satisfied ?
 
     
     
    