Skip to content

Instantly share code, notes, and snippets.

@DmitryBe
Forked from frgomes/CustomIterator.scala
Created April 5, 2017 00:41
Show Gist options
  • Save DmitryBe/6a80d326c47c07b33b0ae6081fa9800e to your computer and use it in GitHub Desktop.
Save DmitryBe/6a80d326c47c07b33b0ae6081fa9800e to your computer and use it in GitHub Desktop.
Scala - Custom Iterator
import scala.collection.AbstractIterator
class CustomIterator[T,U](it: Iterator[T])(f: (T => U)) extends AbstractIterator[U] {
override def hasNext: Boolean = it.hasNext
override def next(): U = f(it.next)
}
private def resourceAsStream(resource: String): Iterator[(String, String)] = {
val stream : java.io.InputStream = getClass.getResourceAsStream(resource)
val it = scala.io.Source.fromInputStream( stream ).getLines
val Regex = raw"<([0-9]+)(\s+)(\w+)(\s+)(.+)".r
new CustomIterator[String, (String,String)](it)({
s => s match {
case Regex(seq, _, name, _, m) => (seq, m)
case _ => null
}
})
}
def test() {
val it = resourceAsStream("/extract_market_data-01.txt")
it.foreach { s => println(s) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment