-
-
Save DmitryBe/6a80d326c47c07b33b0ae6081fa9800e to your computer and use it in GitHub Desktop.
Scala - Custom Iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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