Execute a Markov algorithm: Difference between revisions
Content added Content deleted
(Added Scala) |
(→{{header|Scala}}: script style) |
||
Line 738: | Line 738: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8}} |
{{works with|Scala|2.8}} |
||
<lang scala>import scala. |
<lang scala>import scala.io.Source |
||
⚫ | |||
object MarkovAlgorithm { |
object MarkovAlgorithm { |
||
Line 769: | Line 768: | ||
} |
} |
||
}</lang> |
}</lang> |
||
Script-style, and more concise: |
|||
⚫ | |||
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: |
Sample outputs: |
||
Line 789: | Line 810: | ||
11111111111111111111 |
11111111111111111111 |
||
</pre> |
</pre> |
||
The script is called much in the same way, but with the ".scala" extension added. |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |