Constrained random points on a circle: Difference between revisions

m
→‎{{header|Perl 6}}: added sample output
m (→‎{{header|Perl 6}}: added sample output)
Line 2,196:
my %matrix;
for @range X @range -> ($x, $y) { %matrix{$y}{$x} = ' ' }
%matrix{$_.[1]}{$_.[0]} = '*' for @samples;
%matrix{$_}{@range}.join(' ').say for @range;</lang>
{{out}}
<pre> *
* *
* * * * *
* * * *
* * * * *
* *
* * *
* * * *
*
* * * *
* * *
* * * *
*
* *
* * *
* * * *
* * *
*
* *
* *
* * *
* * *
* *
* * *
* * * * * * *
* * * * * *
* * * *
* *
* * *</pre>
Turning that program completely inside-out and reducing to a single statement with a single non-parameter variable, we get thisanother version, whichthat also works:.
 
This uses, among other things, a 0-based matrix rather than a hash, a <tt>given</tt> on the first line that allows us to print the final value of the matrix straight from its initial declaration, a <tt>for</tt> statement feeding a <tt>for</tt> statement modifier, a lambda that unpacks a single x-y argument into two variables, the functional form of pick rather than the method form, a quasi-list comprehension in the middle loop that filters each <tt>given</tt> with a <tt>when</tt>, precalculated squared limits so we don't have to take the square root, use of X- and X** to subtract and exponentiate both <tt>$x</tt> and <tt>$y</tt> in parallel.
Turning that program completely inside-out and reducing to a single statement with a single non-parameter variable, we get this version, which also works:
 
After the <tt>given do</tt> has loaded up <tt>@matrix</tt> with our circle, the <tt>map</tt> on the first line substitutes a space for any undefined matrix element, and the extra space between elements is supplied by the stringification of the list value, performed by the prefix <tt>~</tt> operator, the unary equivalent of concatenation in Perl&nbsp;6.
 
At this point you would be justified in concluding that we are completely mad. <tt>:-)</tt>
 
<lang perl6>(say ~.map: { $_ // ' ' } for my @matrix) given do
Line 2,207 ⟶ 2,242:
}
</lang>
{{out}}
<pre> * * *
* * *
* * * * * *
* * * * * *
* * * * * *
* * *
* * * * *
* * * *
* *
* *
*
* * *
 
* *
This uses, among other things, a 0-based matrix rather than a hash, a <tt>given</tt> on the first line that allows us to print the final value of the matrix straight from its initial declaration, a <tt>for</tt> statement feeding a <tt>for</tt> statement modifier, a lambda that unpacks a single x-y argument into two variables, the functional form of pick rather than the method form, a quasi-list comprehension in the middle loop that filters each <tt>given</tt> with a <tt>when</tt>, precalculated squared limits so we don't have to take the square root, use of X- and X** to subtract and exponentiate both <tt>$x</tt> and <tt>$y</tt> in parallel.
* * * * * *
* *
 
* * * *
After the <tt>given do</tt> has loaded up <tt>@matrix</tt> with our circle, the <tt>map</tt> on the first line substitutes a space for any undefined matrix element, and the extra space between elements is supplied by the stringification of the list value, performed by the prefix <tt>~</tt> operator, the unary equivalent of concatenation in Perl&nbsp;6.
* * *
 
* * * *
At this point you would be justified in concluding that we are completely mad. <tt>:-)</tt>
* *
* *
* * *
* * * * * *
* * * *
* * * * * *
* * * *
* * * * *
* * *</pre>
 
=={{header|Phix}}==
2,392

edits