amrnt
amrnt

Reputation: 1331

Writing this while-loop in for-loop

I'm working with StanfordNLP to extract data from a parsed Tree.

I'm using Scala for coding.

val tp = TregexPattern.compile("SOME_PATTERN")
val res = tp.matcher("SOME_TREE")

to read the results of this I use

while (res.find()) {
  println(res.getMatch.getLeaves.mkString(" "))
}

I want to rewrite this while-loop in for-loop.

Upvotes: 0

Views: 147

Answers (1)

Eastsun
Eastsun

Reputation: 18859

How about this:

val tp = TregexPattern.compile("SOME_PATTERN")
val res = tp.matcher("SOME_TREE")
for(it <- Iterator.continually(res.getMatch).takeWhile(_ => res.find)) {
  println(it.getLeaves.mkString(" "))
}

Upvotes: 1

Related Questions