Text processing/Max licenses in use: Difference between revisions

m (Arrgh. Move to correct position.)
Line 2,284:
 
=={{header|Scala}}==
===Dumb translated imperative version===
 
 
{{trans|Ada}}
{{trans|ALGOL 68}}
Line 2,310 ⟶ 2,309:
{{trans|Tcl}}
{{trans|Zcl}}
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/lTzl4t0cRFORGs9YWtvgAQa31Q3P5gQ36VcAre5i4I7g Scastie (remote JVM)].
<lang Scala>import java.io.{BufferedReader, InputStreamReader}
import java.net.URL
Line 2,337 ⟶ 2,336:
 
println("Max licenses out: " + max)
println("At time(s): " + dates.mkString(", "))
 
}</lang>
===Smart Imperative Scala with dirty side effects===
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/2bdByPOUSICgRIMx4KJ29A Scastie (remote JVM)].
<lang Scala>import scala.collection.mutable.ListBuffer
 
object License1 extends App {
val src = io.Source.fromURL("https://raw.githubusercontent.com/def-/nim-unsorted/master/mlijobs.txt")
 
val dates = new ListBuffer[String]
var (max, count) = (Int.MinValue, 0)
 
src.getLines.foreach { line =>
def date = line.split(" ")(3)
 
if (line.startsWith("License OUT ")) {
count += 1
if (count > max) {
max = count
dates.clear
}
if (count == max) dates += date
} else if (line.startsWith("License IN ")) count -= 1
}
 
println("Max licenses out: " + max)
println("At time(s): " + dates.mkString(", "))
 
}</lang>
===Clean coded [[functional_programming|FP]] with (tail) recursion, no side effects===
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/MvwfLtnFTcqBoNHwB4aURg Scastie (remote JVM)].
<lang Scala>import scala.annotation.tailrec
 
object License2 extends App {
type resultTuple = (Int /*max*/, Int /*count*/, List[String] /*dates*/ )
 
val src = io.Source.fromURL(
"https://raw.githubusercontent.com/def-/nim-unsorted/master/mlijobs.txt")
val iter = src.getLines()
val (max, count, dates) = loop(Int.MinValue, 0, Nil)
 
def lineToResult(tuple: (resultTuple, String)): resultTuple = {
val ((max, count, dates), line) = tuple
 
def date = line.split(" ")(3)
 
if (line.startsWith("License OUT ")) {
if (count + 1 > max) (count + 1, count + 1, List(date))
else if (count + 1 == max) (max, max, dates :+ date)
else (max, count + 1, dates)
} else if (line.startsWith("License IN ")) tuple._1.copy(_2 = count - 1)
else tuple._1
}
 
@tailrec
private def loop(tuple: resultTuple): resultTuple = {
def lineToResult(tuple: (resultTuple, String)): resultTuple = {
val ((max, count, dates), line) = tuple
 
def date = line.split(" ")(3)
 
if (line.startsWith("License OUT ")) {
if (count + 1 > max) (count + 1, count + 1, List(date))
else if (count + 1 == max) (max, max, dates :+ date)
else (max, count + 1, dates)
} else if (line.startsWith("License IN ")) tuple._1.copy(_2 = count - 1)
else tuple._1
}
 
if (iter.hasNext)
loop(lineToResult(tuple, iter.next()))
else tuple
}
 
println("Max licenses out: " + max)
println("At time(s): " + dates.mkString(", "))
 
}</lang>
===Totally [[functional_programming|FP]] with foldLeft===
{{Out}}Best seen running in your browser [https://scastie.scala-lang.org/h8CgsFx8TJGtRy1B5KVSOg Scastie (remote JVM)].
<lang Scala>object License3 extends App {
type resultTuple = (Int /*max*/, Int /*count*/, List[String] /*dates*/ )
 
val src = io.Source.fromURL("https://raw.githubusercontent.com/def-/nim-unsorted/master/mlijobs.txt")
 
val (max, count, dates): resultTuple =
src.getLines().foldLeft(Int.MinValue, 0, Nil: List[String]) {
case ((max: Int, count: Int, dates: List[String]), line: String)
if line.startsWith("License OUT ") =>
def date = line.split(" ")(3)
 
if (count + 1 > max) (count + 1, count + 1, List(date))
else if (count + 1 == max) (max, max, dates :+ date)
else (max, count + 1, dates)
 
case (resultPart: resultTuple, line: String)
if line.startsWith("License IN ") =>
resultPart.copy(_2 = resultPart._2 - 1)
 
case (resultPart, _) => resultPart
}
 
println("Max licenses out: " + max)
println("At time(s): " + dates.mkString(", "))
 
}</lang>
{{in progress|lang=LANG|day=DD|month=MM|year=YYYY}}
//
 
=={{header|Seed7}}==
Anonymous user