Anonymous user
Execute a Markov algorithm: Difference between revisions
→{{header|Scala}}: script style
(Added Scala) |
(→{{header|Scala}}: script style) |
||
Line 738:
=={{header|Scala}}==
{{works with|Scala|2.8}}
<lang scala>import scala.
import scala.io.Source▼
object MarkovAlgorithm {
Line 769 ⟶ 768:
}
}</lang>
Script-style, and more concise:
▲<lang scala>import scala.io.Source
if (argv.size != 2 ) {
println("Syntax: MarkovAlgorithm inputFile inputPattern")
exit(1)
}
val rulePattern = """(.*?)\s+->\s+(\.?)(.*)""".r
val isComment = (_: String) matches "#.*|\\s*"
val rules = Source fromPath args(0) getLines () filterNot isComment map (rulePattern unapplySeq _ get) toList;
def algorithm(input: String): String = rules find (input contains _.head) match {
case Some(Seq(pattern, ".", replacement)) => input replaceFirst ("\\Q"+pattern+"\\E", replacement)
case Some(Seq(pattern, "", replacement)) => algorithm(input replaceFirst ("\\Q"+pattern+"\\E", replacement))
case None => input
}
println(argv(1))
println(algorithm(argv(1)))</lang>
Sample outputs:
Line 789 ⟶ 810:
11111111111111111111
</pre>
The script is called much in the same way, but with the ".scala" extension added.
=={{header|Tcl}}==
|