scala iterator traversal scala-collections collect

Scala: cómo atravesar stream/iterator recolectando resultados en varias colecciones diferentes



traversal scala-collections (3)

Estoy revisando el archivo de registro que es demasiado grande para caber en la memoria y recopilar 2 tipos de expresiones, ¿cuál es la mejor alternativa funcional a mi fragmento iterativo a continuación?

def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String, String)]={ val lines : Iterator[String] = io.Source.fromFile(file).getLines() val logins: mutable.Map[String, String] = new mutable.HashMap[String, String]() val errors: mutable.ListBuffer[(String, String)] = mutable.ListBuffer.empty for (line <- lines){ line match { case errorPat(date,ip)=> errors.append((ip,date)) case loginPat(date,user,ip,id) =>logins.put(ip, id) case _ => "" } } errors.toList.map(line => (logins.getOrElse(line._1,"none") + " " + line._1,line._2)) }


Aquí hay una posible solución:

def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String,String)] = { val lines = Source.fromFile(file).getLines val (err, log) = lines.collect { case errorPat(inf, ip) => (Some((ip, inf)), None) case loginPat(_, _, ip, id) => (None, Some((ip, id))) }.toList.unzip val ip2id = log.flatten.toMap err.collect{ case Some((ip,inf)) => (ip2id.getOrElse(ip,"none") + "" + ip, inf) } }


Correcciones
1) eliminó las declaraciones de tipos innecesarios
2) deconstrucción de tuplas en lugar de ulical ._1
3) pliegue izquierdo en lugar de acumuladores mutables
4) utilizó métodos más convenientes tipo operador :+ y +

def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String, String)] = { val lines = io.Source.fromFile(file).getLines() val (logins, errors) = ((Map.empty[String, String], Seq.empty[(String, String)]) /: lines) { case ((loginsAcc, errorsAcc), next) => next match { case errorPat(date, ip) => (loginsAcc, errorsAcc :+ (ip -> date)) case loginPat(date, user, ip, id) => (loginsAcc + (ip -> id) , errorsAcc) case _ => (loginsAcc, errorsAcc) } } // more concise equivalent for // errors.toList.map { case (ip, date) => (logins.getOrElse(ip, "none") + " " + ip) -> date } for ((ip, date) <- errors.toList) yield (logins.getOrElse(ip, "none") + " " + ip) -> date }


Tengo algunas sugerencias:

  • En lugar de un par / tupla, a menudo es mejor usar tu propia clase. Da nombres significativos tanto para el tipo como para sus campos, lo que hace que el código sea mucho más legible.
  • Divida el código en partes pequeñas. En particular, intente desacoplar las piezas de código que no necesitan vincularse. Esto hace que su código sea más fácil de entender, más robusto, menos propenso a errores y más fácil de probar. En su caso, sería bueno separar la producción de su entrada (líneas de un archivo de registro) y consumirla para producir un resultado. Por ejemplo, podría realizar pruebas automáticas para su función sin tener que almacenar datos de muestra en un archivo.

Como ejemplo y ejercicio, traté de hacer una solución basada en iteraciones de Scalaz. Es un poco más largo (incluye algún código auxiliar para IteratorEnumerator ) y quizás sea un poco exagerado para la tarea, pero quizás alguien lo encuentre útil.

import java.io._; import scala.util.matching.Regex import scalaz._ import scalaz.IterV._ object MyApp extends App { // A type for the result. Having names keeps things // clearer and shorter. type LogResult = List[(String,String)] // Represents a state of our computation. Not only it // gives a name to the data, we can also put here // functions that modify the state. This nicely // separates what we''re computing and how. sealed case class State( logins: Map[String,String], errors: Seq[(String,String)] ) { def this() = { this(Map.empty[String,String], Seq.empty[(String,String)]) } def addError(date: String, ip: String): State = State(logins, errors :+ (ip -> date)); def addLogin(ip: String, id: String): State = State(logins + (ip -> id), errors); // Produce the final result from accumulated data. def result: LogResult = for ((ip, date) <- errors.toList) yield (logins.getOrElse(ip, "none") + " " + ip) -> date } // An iteratee that consumes lines of our input. Based // on the given regular expressions, it produces an // iteratee that parses the input and uses State to // compute the result. def logIteratee(errorPat: Regex, loginPat: Regex): IterV[String,List[(String,String)]] = { // Consumes a signle line. def consume(line: String, state: State): State = line match { case errorPat(date, ip) => state.addError(date, ip); case loginPat(date, user, ip, id) => state.addLogin(ip, id); case _ => state } // The core of the iteratee. Every time we consume a // line, we update our state. When done, compute the // final result. def step(state: State)(s: Input[String]): IterV[String, LogResult] = s(el = line => Cont(step(consume(line, state))), empty = Cont(step(state)), eof = Done(state.result, EOF[String])) // Return the iterate waiting for its first input. Cont(step(new State())); } // Converts an iterator into an enumerator. This // should be more likely moved to Scalaz. // Adapted from scalaz.ExampleIteratee implicit val IteratorEnumerator = new Enumerator[Iterator] { @annotation.tailrec def apply[E, A](e: Iterator[E], i: IterV[E, A]): IterV[E, A] = { val next: Option[(Iterator[E], IterV[E, A])] = if (e.hasNext) { val x = e.next(); i.fold(done = (_, _) => None, cont = k => Some((e, k(El(x))))) } else None; next match { case None => i case Some((es, is)) => apply(es, is) } } } // main --------------------------------------------------- { // Read a file as an iterator of lines: // val lines: Iterator[String] = // io.Source.fromFile("test.log").getLines(); // Create our testing iterator: val lines: Iterator[String] = Seq( "Error: 2012/03 1.2.3.4", "Login: 2012/03 user 1.2.3.4 Joe", "Error: 2012/03 1.2.3.5", "Error: 2012/04 1.2.3.4" ).iterator; // Create an iteratee. val iter = logIteratee("Error: (//S+) (//S+)".r, "Login: (//S+) (//S+) (//S+) (//S+)".r); // Run the the iteratee against the input // (the enumerator is implicit) println(iter(lines).run); } }