Price fraction: Difference between revisions

→‎{{header|Perl 6}}: reverse the order of the three solutions
(→‎{{header|Perl 6}}: Clean up the "chained conditionals" solution, and use `when` instead of the ternary operator.)
(→‎{{header|Perl 6}}: reverse the order of the three solutions)
Line 2,097:
 
=={{header|Perl 6}}==
 
Simple solution, doing a linear search.<br>
Note that in Perl&nbsp;6 we don't have to worry about floating-point misrepresentations of decimals, because decimal fractions are stored as rationals.
 
{{works with|rakudo|2016.07}}
<lang perl6>sub price-fraction ($n where 0..1) {
when $n < 0.06 { 0.10 }
when $n < 0.11 { 0.18 }
when $n < 0.16 { 0.26 }
when $n < 0.21 { 0.32 }
when $n < 0.26 { 0.38 }
when $n < 0.31 { 0.44 }
when $n < 0.36 { 0.50 }
when $n < 0.41 { 0.54 }
when $n < 0.46 { 0.58 }
when $n < 0.51 { 0.62 }
when $n < 0.56 { 0.66 }
when $n < 0.61 { 0.70 }
when $n < 0.66 { 0.74 }
when $n < 0.71 { 0.78 }
when $n < 0.76 { 0.82 }
when $n < 0.81 { 0.86 }
when $n < 0.86 { 0.90 }
when $n < 0.91 { 0.94 }
when $n < 0.96 { 0.98 }
default { 1.00 }
}
 
while prompt("value: ") -> $value {
say price-fraction(+$value);
}</lang>
 
If we expect to rescale many prices, a better approach would be to build a look-up table using an array of 101 entries.
Memory is cheap, and array lookup is blazing fast.
 
<lang perl6>my @price = map *.value, flat
( 0 ..^ 6 X=> 0.10),
( 6 ..^ 11 X=> 0.18),
(11 ..^ 16 X=> 0.26),
(16 ..^ 21 X=> 0.32),
(21 ..^ 26 X=> 0.38),
(26 ..^ 31 X=> 0.44),
(31 ..^ 36 X=> 0.50),
(36 ..^ 41 X=> 0.54),
(41 ..^ 46 X=> 0.58),
(46 ..^ 51 X=> 0.62),
(51 ..^ 56 X=> 0.66),
(56 ..^ 61 X=> 0.70),
(61 ..^ 66 X=> 0.74),
(66 ..^ 71 X=> 0.78),
(71 ..^ 76 X=> 0.82),
(76 ..^ 81 X=> 0.86),
(81 ..^ 86 X=> 0.90),
(86 ..^ 91 X=> 0.94),
(91 ..^ 96 X=> 0.98),
(96 ..^101 X=> 1.00),
;
 
while prompt("value: ") -> $value {
say @price[ $value * 100 ] // "Out of range";
}</lang>
 
{{works with|rakudo|2015-10-20}}
<lang perl6>my $table = q:to/END/;
Line 2,132 ⟶ 2,194:
}
fail "Out of range";
}</lang>
Perhaps a better approach is just to build an array of 101 entries.
Memory is cheap, and array lookup is blazing fast, especially important if used in a loop as below.
Moreover, in Perl&nbsp;6 we don't have to worry about floating point misrepresentations of decimals because decimal fractions are stored as rationals.
 
<lang perl6>my @price = map *.value, flat
( 0 ..^ 6 X=> 0.10),
( 6 ..^ 11 X=> 0.18),
(11 ..^ 16 X=> 0.26),
(16 ..^ 21 X=> 0.32),
(21 ..^ 26 X=> 0.38),
(26 ..^ 31 X=> 0.44),
(31 ..^ 36 X=> 0.50),
(36 ..^ 41 X=> 0.54),
(41 ..^ 46 X=> 0.58),
(46 ..^ 51 X=> 0.62),
(51 ..^ 56 X=> 0.66),
(56 ..^ 61 X=> 0.70),
(61 ..^ 66 X=> 0.74),
(66 ..^ 71 X=> 0.78),
(71 ..^ 76 X=> 0.82),
(76 ..^ 81 X=> 0.86),
(81 ..^ 86 X=> 0.90),
(86 ..^ 91 X=> 0.94),
(91 ..^ 96 X=> 0.98),
(96 ..^101 X=> 1.00),
;
 
while prompt("value: ") -> $value {
say @price[ $value * 100 ] // note "Out of range";
}</lang>
 
Yet another approach is to use <tt>when</tt> statements to encode the table.
This allows each endpoint to be written once, avoiding duplication.
 
<lang perl6>sub price-fraction ($n where 0..1) {
when $n < 0.06 { 0.10 }
when $n < 0.11 { 0.18 }
when $n < 0.16 { 0.26 }
when $n < 0.21 { 0.32 }
when $n < 0.26 { 0.38 }
when $n < 0.31 { 0.44 }
when $n < 0.36 { 0.50 }
when $n < 0.41 { 0.54 }
when $n < 0.46 { 0.58 }
when $n < 0.51 { 0.62 }
when $n < 0.56 { 0.66 }
when $n < 0.61 { 0.70 }
when $n < 0.66 { 0.74 }
when $n < 0.71 { 0.78 }
when $n < 0.76 { 0.82 }
when $n < 0.81 { 0.86 }
when $n < 0.86 { 0.90 }
when $n < 0.91 { 0.94 }
when $n < 0.96 { 0.98 }
default { 1.00 }
}
 
while prompt("value: ") -> $value {
last if $value ~~ /exit|quit/;
say price-fraction(+$value);
}</lang>
 
Anonymous user