Rosetta Code/Find bare lang tags: Difference between revisions
Content added Content deleted
m (→{{header|Ruby}}: Hash.new takes 2 params, not three) |
|||
Line 425: | Line 425: | ||
1 in PHP (["Greatest_subsequential_sum"]) |
1 in PHP (["Greatest_subsequential_sum"]) |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
|||
To analyse RosettaCode pages, invoke Java with <code>-Dhttp.agent=Anything</code> to work around CloudFlare blocking Java from accessing the RosettaCode site. |
|||
<lang Scala>// Map lines to a list of Option(heading -> task) for each bare lang tag found. |
|||
val headerFormat = "==[{]+header[|]([^}]*)[}]+==".r |
|||
val langFormat = "<lang([^>]*)>".r |
|||
def mapped(lines: Seq[String], taskName: String = "") = { |
|||
var heading = "" |
|||
for (line <- lines; |
|||
head = headerFormat.findFirstMatchIn(line).map(_ group 1); |
|||
lang = langFormat.findFirstMatchIn(line).map(_ group 1)) yield { |
|||
if (head.isDefined) heading = head.get |
|||
lang.map(_.trim).filter(_ == "").map(_ => heading -> taskName) |
|||
} |
|||
} |
|||
// Group results as a Map(heading -> task1, task2, ...) |
|||
def reduced(results: Seq[Option[(String,String)]]) = |
|||
results.flatten.groupBy(_._1).mapValues(_.unzip._2) |
|||
// Format each heading as "tasklist.size in heading (tasklist)" |
|||
def format(results: Map[String,Seq[String]]) = results.map{case (heading, tasks) => |
|||
val h = if (heading.length > 0) heading else "no langauge" |
|||
val hmsg = s"${tasks.size} in $h" |
|||
val t = tasks.filterNot(_ == "") |
|||
val tmsg = if (t.isEmpty) "" else t.distinct.mkString(" (", ",", ")") |
|||
hmsg + tmsg |
|||
} |
|||
def count(results: Map[String,Seq[String]]) = results.values.map(_.size).sum |
|||
// Single and multi-source support |
|||
case class BareLangFinder(source: scala.io.Source, taskName: String = "") { |
|||
def map = mapped(source.getLines.toSeq, taskName) |
|||
def mapReduce = reduced(map) |
|||
def summary = format(mapReduce) mkString "\n" |
|||
} |
|||
def mapReduce(inputs: Seq[BareLangFinder]) = reduced(inputs.flatMap(_.map))</lang> |
|||
'''Examples:''' |
|||
<pre>val test = """ |
|||
Description |
|||
<lang>Pseudocode</lang> |
|||
=={{header|C}}== |
|||
<lang C>printf("Hello world!\n");</lang> |
|||
=={{header|Perl}}== |
|||
<lang>print "Hello world!\n"</lang> |
|||
""" |
|||
println(BareLangFinder(scala.io.Source.fromString(test)).summary) |
|||
// System.setProperty("http.agent", "RosettaCode/1.0") |
|||
val tasks = List("Greatest_common_divisor", "Greatest_element_of_a_list", "Greatest_subsequential_sum") |
|||
val inputs = for (task <- tasks; url = "http://rosettacode.org/wiki?action=raw&title=" + task) |
|||
yield BareLangFinder(scala.io.Source.fromURL(url), task) |
|||
val bare = mapReduce(inputs) |
|||
println |
|||
println(s"${count(bare)} bare language tags in ${tasks.size} tasks:") |
|||
println(format(bare) mkString "\n")</pre> |
|||
{{out}} |
|||
<pre>1 in Perl |
|||
1 in no langauge |
|||
10 bare language tags in 3 tasks: |
|||
2 in Mathprog (Greatest_subsequential_sum) |
|||
1 in gnuplot (Greatest_common_divisor) |
|||
2 in МК-61/52 (Greatest_element_of_a_list) |
|||
1 in Bracmat (Greatest_element_of_a_list) |
|||
1 in PHP (Greatest_subsequential_sum) |
|||
2 in Euler Math Toolbox (Greatest_common_divisor,Greatest_element_of_a_list) |
|||
1 in ooRexx (Greatest_element_of_a_list)</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |