Execute a Markov algorithm: Difference between revisions
Content added Content deleted
(Added OCaml solution) |
(Updated D entry) |
||
Line 1,383: | Line 1,383: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
⚫ | |||
<lang d>import std.stdio, std.array, std.file, std.regex, std.string, |
|||
std.range |
import std.stdio, std.file, std.regex, std.string, std.range, |
||
std.functional; |
|||
const rules = "markov_rules.txt".readText.splitLines.split(""); |
|||
⚫ | |||
auto tests = "markov_tests.txt".readText.splitLines; |
|||
⚫ | |||
.split(""); |
|||
auto tests = readText("markov_tests.txt").splitLines(); |
|||
⚫ | |||
alias slZip = curry!(zip, StoppingPolicy.requireSameLength); |
|||
foreach (test, rule; |
foreach (test, const rule; slZip(tests, rules)) { |
||
const origTest = test.dup; |
|||
string[][] capt; |
string[][] capt; |
||
foreach (line; rule) { |
foreach (const line; rule) { |
||
auto m = line.match(re); |
auto m = line.match(re); |
||
if (!m.empty) |
if (!m.empty) { |
||
capt |
//capt.put(m.captures.dropOne); |
||
capt ~= m.captures.dropOne.array; |
|||
} |
|||
} |
} |
||
REDO: |
REDO: |
||
const copy = test; |
|||
foreach (c; capt) { |
foreach (const c; capt) { |
||
test = test.replace(c[0], c[2]); |
test = test.replace(c[0], c[2]); |
||
if (c[1] == ".") |
if (c[1] == ".") |