Knuth's algorithm S: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 465:
9 30255
</lang>
 
 
=={{header|Common Lisp}}==
Line 667 ⟶ 666:
using an input list -> [|(0, 29936); (1, 29973); (2, 29880); (3, 30160); (4, 30126); (5, 30123); (6, 30062); (7, 30053); (8, 29892); (9, 29795)|]
</pre>
 
=={{header|Go}}==
<lang go>package main
Line 1,218:
;Sample output:
<pre>30003 29923 30192 30164 29994 29976 29935 29860 30040 29913</pre>
=={{header|Perl 6}}==
<lang perl6>sub s_of_n_creator($n) {
my @sample;
my $i = 0;
-> $item {
if ++$i <= $n {
push @sample, $item;
}
elsif $i.rand < $n {
@sample[$n.rand] = $item;
}
@sample;
}
}
 
my @items = 0..9;
my @bin;
 
for ^100000 {
my &s_of_n = s_of_n_creator(3);
my @sample;
for @items -> $item {
@sample = s_of_n($item);
}
for @sample -> $s {
@bin[$s]++;
}
}
say @bin;</lang>
Output:
<pre>29975 30028 30246 30056 30004 29983 29836 29967 29924 29981</pre>
 
=={{header|Phix}}==
Line 1,494 ⟶ 1,463:
8 29940
9 29777</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub s_of_n_creator($n) {
my @sample;
my $i = 0;
-> $item {
if ++$i <= $n {
push @sample, $item;
}
elsif $i.rand < $n {
@sample[$n.rand] = $item;
}
@sample;
}
}
 
my @items = 0..9;
my @bin;
 
for ^100000 {
my &s_of_n = s_of_n_creator(3);
my @sample;
for @items -> $item {
@sample = s_of_n($item);
}
for @sample -> $s {
@bin[$s]++;
}
}
say @bin;</lang>
Output:
<pre>29975 30028 30246 30056 30004 29983 29836 29967 29924 29981</pre>
 
=={{header|REXX}}==
Line 1,584 ⟶ 1,586:
8 29953
9 29852</pre>
=={{header|Scala}}==
===Imperative (Ugly and side effects)===
{{trans|Java}}
<lang Scala>import java.util
import scala.util.Random
 
object KnuthsAlgorithmS extends App {
 
import scala.collection.JavaConverters._
 
val (n, rand, bin) = (3, Random, new Array[Int](10))
 
for (_ <- 0 until 100000) {
val sample = new util.ArrayList[Int](n)
for (item <- 0 until 10) {
if (item < n) sample.add(item)
else if (rand.nextInt(item + 1) < n)
sample.asScala(rand.nextInt(n)) = item
}
for (s <- sample.asScala.toList) bin(s) += 1
}
 
println(bin.mkString("[", ", ", "]"))
}</lang>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/nlldfXD/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/WLaee5H9T72cximqK9gECA Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>func s_of_n_creator(n) {
var i = 0
var sample = []
{ |item|
if (++i <= n) {
sample << item;
}
elsif (i.rand < n) {
sample[n.rand] = item;
}
sample;
}
}
 
var items = 0..9;
var bin = [];
 
100000.times {
var s_of_n = s_of_n_creator(3);
var sample = []
for item in items {
sample = s_of_n(item);
}
for s in sample {
bin[s] := 0 ++;
}
}
 
say bin;</lang>
{{out}}
<pre>
[30056, 29906, 30058, 29986, 30062, 29748, 29989, 29985, 30126, 30084]
</pre>
 
=={{header|Rust}}==
Line 1,709 ⟶ 1,650:
frequency of 8: 30030
frequency of 9: 30003
</pre>
 
=={{header|Scala}}==
===Imperative (Ugly and side effects)===
{{trans|Java}}
<lang Scala>import java.util
import scala.util.Random
 
object KnuthsAlgorithmS extends App {
 
import scala.collection.JavaConverters._
 
val (n, rand, bin) = (3, Random, new Array[Int](10))
 
for (_ <- 0 until 100000) {
val sample = new util.ArrayList[Int](n)
for (item <- 0 until 10) {
if (item < n) sample.add(item)
else if (rand.nextInt(item + 1) < n)
sample.asScala(rand.nextInt(n)) = item
}
for (s <- sample.asScala.toList) bin(s) += 1
}
 
println(bin.mkString("[", ", ", "]"))
}</lang>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/nlldfXD/0 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/WLaee5H9T72cximqK9gECA Scastie (JVM)].
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>func s_of_n_creator(n) {
var i = 0
var sample = []
{ |item|
if (++i <= n) {
sample << item;
}
elsif (i.rand < n) {
sample[n.rand] = item;
}
sample;
}
}
 
var items = 0..9;
var bin = [];
 
100000.times {
var s_of_n = s_of_n_creator(3);
var sample = []
for item in items {
sample = s_of_n(item);
}
for s in sample {
bin[s] := 0 ++;
}
}
 
say bin;</lang>
{{out}}
<pre>
[30056, 29906, 30058, 29986, 30062, 29748, 29989, 29985, 30126, 30084]
</pre>
 
10,333

edits