Anonymous user
Decision tables: Difference between revisions
Improved and updated D entry
m (→{{header|J}}) |
(Improved and updated D entry) |
||
Line 194:
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.exception, std.string,
std.array, std.conv;
struct
alias immutable(
immutable
immutable IBA[IBA] rules ;▼
private static immutable(bool[]) growTo(in bool[] b,
in size_t newLen)
conds = c.idup ;▼
pure nothrow {
actions = a.idup ;▼
return
p[1].length = actions.length ;▼
r[p[0].idup] = p[1].idup ;▼
}▼
rules = assumeUnique(r) ;▼
}
this(in string[]
const pure nothrow {
foreach (p; q)
}
string[] test(byte[] tested, string NoMatchMsg = "it is fine :)") {▼
string[] test(in bool[] tested,
string[] rightActions ;▼
▲ tested.length = conds.length ;
const pure /*nothrow*/ {
auto iTested =
if (iTested in
foreach (i, immutable(byte) e;
return [NoMatchMsg] ;▼
return rightActions;
}
void consult() const {
bool[] query
foreach (
write(
} catch (StdioException) {
▲ }
query ~= !!answer.startsWith('y', 'Y');
}
▲ writeln(test(query).join("\n")) ;
test(query).join("\n").writeln();
}
}
void main() {
const d = DecisionTable(
["Printer is unrecognised",
"A red light is flashing",
"Printer does not print"],
["Check the power cable",
"Check the printer-computer cable",
Line 253 ⟶ 266:
"Check/replace ink",
"Check for paper jam"],
[[[1,0,0],[0,0,1]],▼
[[
[[
[[
[[
[[
]
)
d.consult()
}</lang>
{{out}}
<pre>Printer is unrecognised? [y=yes/others=no]
A red light is flashing? [y=yes/others=no]
Printer does not print? [y=yes/others=no]
It is fine :)</pre>
=={{header|Go}}==
Go has no specific support for decision tables, but they can be implemented easily. With a little ingenuity, literal data can be arranged in rows and columns in a way that preserves the visual associations of decision tables. Go has an init function that might be useful for compiling decision tables at program startup. And Go maps allow efficient lookup of actions given conditions.
|