Decision tables: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 11: Line 11:
Use this [[wp:Decision_table#Example|example]] of Printer Troubleshooting given in the Wikipedia article.
Use this [[wp:Decision_table#Example|example]] of Printer Troubleshooting given in the Wikipedia article.
<br><br>
<br><br>



=={{header|Ada}}==
=={{header|Ada}}==
Line 231: Line 230:
GuiControl,, Output, % Res
GuiControl,, Output, % Res
return</lang>
return</lang>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>
Line 849: Line 849:


For large numbers of rules and few actions, J's native support of sparse arrays might provide a performance advantage, particularly in space. A minor note about the implementation here: the verb (function) <tt>troubleshoot</tt> is generalized, and reusable on any set of rule table, questions, and suggested actions. The default is to use those given in the printer troubleshooting table.
For large numbers of rules and few actions, J's native support of sparse arrays might provide a performance advantage, particularly in space. A minor note about the implementation here: the verb (function) <tt>troubleshoot</tt> is generalized, and reusable on any set of rule table, questions, and suggested actions. The default is to use those given in the printer troubleshooting table.




=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 1,025: Line 1,023:
Check/replace ink.
Check/replace ink.
</lang>
</lang>



=={{header|Julia}}==
=={{header|Julia}}==
Line 1,176: Line 1,173:
Printer does not print: y
Printer does not print: y
Check for paper jam</pre>
Check for paper jam</pre>

=={{header|Perl 6}}==
<lang perl6>sub decide (@q, @s) {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
}
say " $_" for @s.grep(*.key +& $bit)».value || "No clue!";
}

loop {
decide
(
"Y Y Y Y N N N N" => "Printer does not print",
"Y Y N N Y Y N N" => "A red light is flashing",
"Y N Y N Y N Y N" => "Printer is unrecognised",
),
(
:2<0_0_1_0_0_0_0_0> => "Check the power cable",
:2<1_0_1_0_0_0_0_0> => "Check the printer-computer cable",
:2<1_0_1_0_1_0_1_0> => "Ensure printer software is installed",
:2<1_1_0_0_1_1_0_0> => "Check/replace ink",
:2<0_1_0_1_0_0_0_0> => "Check for paper jam",
);
say '';
}</lang>
A bit of explanation: we pass in two pair lists for the questions and solutions; we ignore the keys of the questions, since they can be
generated by regarding them as a binary counter from right to left, with the least significant bit on the bottom. The <tt>@q.map</tt> runs the prompts and maps them to booleans
using case-insensitive matching. We reverse that list and zip multiply with powers of two to figure out which bit we're going to grep for. (The zip stops on the shorter list, which is always going to be the list of booleans, since the list of powers of two is infinite.) We sum up those powers of two using a <tt>[+]</tt> reduction metaoperator, which in this case gives us a number from 0 to 7. Then we take 2 to that power.

The solutions list of pairs is conveniently keyed on binary numbers written in colon radix notation, so we grep the keys containing the correct bit, then map the pair list to its values using a hyperoperator to parallelize it. Unlike in Perl 5, we can use <tt>||</tt> on lists as well as scalars to provide a default result if nothing matches.
{{out}}
<pre>Printer does not print? n
A red light is flashing? y
Printer is unrecognised? n
Check/replace ink

Printer does not print? y
A red light is flashing? n
Printer is unrecognised? y
Check the power cable
Check the printer-computer cable
Ensure printer software is installed

Printer does not print? n
A red light is flashing? n
Printer is unrecognised? n
No clue!

Printer does not print? ^C</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,572: Line 1,520:
Printer is unrecognized [y/n]?n
Printer is unrecognized [y/n]?n
Suggested action: (Check/replace ink)</pre>
Suggested action: (Check/replace ink)</pre>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub decide (@q, @s) {
my $bit = 2 ** [+] (1,2,4...*) Z* reverse @q.map: {
so prompt(.value ~ "? ") ~~ /:i ^y/;
}
say " $_" for @s.grep(*.key +& $bit)».value || "No clue!";
}

loop {
decide
(
"Y Y Y Y N N N N" => "Printer does not print",
"Y Y N N Y Y N N" => "A red light is flashing",
"Y N Y N Y N Y N" => "Printer is unrecognised",
),
(
:2<0_0_1_0_0_0_0_0> => "Check the power cable",
:2<1_0_1_0_0_0_0_0> => "Check the printer-computer cable",
:2<1_0_1_0_1_0_1_0> => "Ensure printer software is installed",
:2<1_1_0_0_1_1_0_0> => "Check/replace ink",
:2<0_1_0_1_0_0_0_0> => "Check for paper jam",
);
say '';
}</lang>
A bit of explanation: we pass in two pair lists for the questions and solutions; we ignore the keys of the questions, since they can be
generated by regarding them as a binary counter from right to left, with the least significant bit on the bottom. The <tt>@q.map</tt> runs the prompts and maps them to booleans
using case-insensitive matching. We reverse that list and zip multiply with powers of two to figure out which bit we're going to grep for. (The zip stops on the shorter list, which is always going to be the list of booleans, since the list of powers of two is infinite.) We sum up those powers of two using a <tt>[+]</tt> reduction metaoperator, which in this case gives us a number from 0 to 7. Then we take 2 to that power.

The solutions list of pairs is conveniently keyed on binary numbers written in colon radix notation, so we grep the keys containing the correct bit, then map the pair list to its values using a hyperoperator to parallelize it. Unlike in Perl 5, we can use <tt>||</tt> on lists as well as scalars to provide a default result if nothing matches.
{{out}}
<pre>Printer does not print? n
A red light is flashing? y
Printer is unrecognised? n
Check/replace ink

Printer does not print? y
A red light is flashing? n
Printer is unrecognised? y
Check the power cable
Check the printer-computer cable
Ensure printer software is installed

Printer does not print? n
A red light is flashing? n
Printer is unrecognised? n
No clue!

Printer does not print? ^C</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 1,958: Line 1,956:
Printer does not print? ^C
Printer does not print? ^C
</pre>
</pre>

=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>package require TclOO
<lang tcl>package require TclOO