Muster-match in der foreach und dann einen endgültigen Schritt

Ist es möglich, etwas zu tun, nachdem Sie ein pattern match foreach Aussage?

Ich will ein post-match-Schritt z.B. um eine variable zu setzen. Ich möchte auch die Kraft eine Einheit zurück, wie meine foreach ist String => Einheit, und standardmäßig Scala will return die Letzte Anweisung.

Hier einige code:

    Iteratee.foreach[String](_ match {
      case "date" => out.push("Current date: " + new Date().toString + "<br/>")
      case "since" => out.push("Last command executed: " + (ctm - last) + "ms before now<br/>")
      case unknow => out.push("Command: " + unknown + " not recognized <br/>")
    } //here I would like to set "last = ctm" (will be a Long) 
    ) 

AKTUALISIERT:
Neuen code und Kontext. Auch neue Fragen Hinzugefügt 🙂 Sie sind eingebettet in den Kommentaren.

def socket = WebSocket.using[String] { request =>

 //Comment from an answer bellow but what are the side effects?
 //By convention, methods with side effects takes an empty argument list
 def ctm(): Long = System.currentTimeMillis

 var last: Long = ctm

 //Command handlers
 //Comment from an answer bellow but what are the side effects?
 //By convention, methods with side effects takes an empty argument list
 def date() = "Current date: " + new Date().toString + "<br/>"
 def since(last: Long) = "Last command executed: " + (ctm - last) + "ms before now<br/>"
 def unknown(cmd: String) = "Command: " + cmd + " not recognized <br/>"

 val out = Enumerator.imperative[String] {}

 //How to transform into the mapping strategy given in lpaul7's nice answer.
 lazy val in = Iteratee.foreach[String](_ match {
   case "date" => out.push(date)
   case "since" => out.push(since(last))
   case unknown => out.push(unknown)
 } //Here I want to update the variable last to "last = ctm"
 ).mapDone { _ =>
   println("Disconnected")
 }

 (in, out)
}

InformationsquelleAutor Farmor | 2012-07-03

Schreibe einen Kommentar