Last active
April 5, 2017 00:41
-
-
Save frgomes/c0bb2352e77be743ed6456604e02281d 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