wolfgang
wolfgang

Reputation: 7819

How to iterate through lazy iterable in scala? from stanford-tmt

Scala newbie here,

I'm using stanford's topic modelling toolkit

and it has a lazy iterable of type LazyIterable[(String, Array[Double])]

How should i iterate through all the elements in this iterable say it to print all these values?

I tried doing this by

while(it.hasNext){

System.out.println(it.next())

}

Gives an error

 error: value next is not a member of scalanlp.collection.LazyIterable[(String, Array[Double])]

This is the API source -> iterable_name -> InferCVB0DocumentTopicDistributions in

http://nlp.stanford.edu/software/tmt/tmt-0.4/api/edu/stanford/nlp/tmt/stage/package.html

Upvotes: 1

Views: 537

Answers (2)

Zoltán
Zoltán

Reputation: 22196

Based on its source code, I can see that the LazyIterable implements the standard Scala Iterable interface, which means you have access to all the standard higher-order functions that all Scala collections implement - such as map, flatMap, filter, etc.

The one you will be interested in for printing all the values is foreach. So try this (no need for the while-loop):

it.foreach(println)

Upvotes: 2

Allen Chou
Allen Chou

Reputation: 1237

Seems like method invocation problem, just check the source code of LazyIterable, look at line 46

override def iterator : Iterator[A]

when you get an instance of LazyIterable, invoke iterator method, then you can do what you want.

Upvotes: 1

Related Questions