YAPC::EU 2018 Glasgow Update!

This commit is contained in:
Ingy döt Net 2018-08-17 15:15:24 +01:00
parent 22f33d4004
commit 4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions

View file

@ -6,32 +6,30 @@ class SOfN<T> {
private List<T> sample;
private int i = 0;
private int n;
public SOfN(int _n) {
n = _n;
sample = new ArrayList<T>(n);
n = _n;
sample = new ArrayList<T>(n);
}
public List<T> process(T item) {
i++;
if (i <= n) {
if (++i <= n) {
sample.add(item);
} else if (rand.nextInt(i) < n) {
sample.set(rand.nextInt(n), item);
}
return sample;
} else if (rand.nextInt(i) < n) {
sample.set(rand.nextInt(n), item);
}
return sample;
}
}
public class AlgorithmS {
public static void main(String[] args) {
int[] bin = new int[10];
for (int trial = 0; trial < 100000; trial++) {
SOfN<Integer> s_of_n = new SOfN<Integer>(3);
List<Integer> sample = null;
for (int i = 0; i < 10; i++)
sample = s_of_n.process(i);
for (int s : sample)
bin[s]++;
}
System.out.println(Arrays.toString(bin));
int[] bin = new int[10];
for (int trial = 0; trial < 100000; trial++) {
SOfN<Integer> s_of_n = new SOfN<Integer>(3);
for (int i = 0; i < 9; i++) s_of_n.process(i);
for (int s : s_of_n.process(9)) bin[s]++;
}
System.out.println(Arrays.toString(bin));
}
}

View file

@ -7,31 +7,27 @@ interface Function<S, T> {
public class AlgorithmS {
private static final Random rand = new Random();
public static <T> Function<T, List<T>> s_of_n_creator(final int n) {
return new Function<T, List<T>>() {
private List<T> sample = new ArrayList<T>(n);
private int i = 0;
public List<T> call(T item) {
i++;
if (i <= n) {
sample.add(item);
} else if (rand.nextInt(i) < n) {
sample.set(rand.nextInt(n), item);
}
return sample;
}
};
return new Function<T, List<T>>() {
private List<T> sample = new ArrayList<T>(n);
private int i = 0;
public List<T> call(T item) {
if (++i <= n) {
sample.add(item);
} else if (rand.nextInt(i) < n) {
sample.set(rand.nextInt(n), item);
}
return sample;
}
};
}
public static void main(String[] args) {
int[] bin = new int[10];
for (int trial = 0; trial < 100000; trial++) {
Function<Integer, List<Integer>> s_of_n = s_of_n_creator(3);
List<Integer> sample = null;
for (int i = 0; i < 10; i++)
sample = s_of_n.call(i);
for (int s : sample)
bin[s]++;
}
System.out.println(Arrays.toString(bin));
int[] bin = new int[10];
for (int trial = 0; trial < 100000; trial++) {
Function<Integer, List<Integer>> s_of_n = s_of_n_creator(3);
for (int i = 0; i < 9; i++) s_of_n.call(i);
for (int s : s_of_n.call(9)) bin[s]++;
}
System.out.println(Arrays.toString(bin));
}
}

View file

@ -1,16 +1,14 @@
// version 1.1.2
// version 1.2.51
import java.util.Random
val rand = Random()
class SOfN<T>(val n: Int) {
private val sample = ArrayList<T>(n)
private var i = 0
private companion object {
val rand = Random()
}
fun process(item: T): ArrayList<T> {
fun process(item: T): List<T> {
if (++i <= n)
sample.add(item)
else if (rand.nextInt(i) < n)
@ -23,9 +21,8 @@ fun main(args: Array<String>) {
val bin = IntArray(10)
(1..100_000).forEach {
val sOfn = SOfN<Int>(3)
var sample: ArrayList<Int>? = null
for (i in 0..9) sample = sOfn.process(i)
for (s in sample!!) bin[s]++
for (d in 0..8) sOfn.process(d)
for (s in sOfn.process(9)) bin[s]++
}
println(bin.contentToString())
}

View file

@ -0,0 +1,27 @@
// version 1.2.51
import java.util.Random
val rand = Random()
fun <T> SOfNCreator(n: Int): (T) -> List<T> {
val sample = ArrayList<T>(n)
var i = 0
return {
if (++i <= n)
sample.add(it)
else if (rand.nextInt(i) < n)
sample[rand.nextInt(n)] = it
sample
}
}
fun main(args: Array<String>) {
val bin = IntArray(10)
(1..100_000).forEach {
val sOfn = SOfNCreator<Int>(3)
for (d in 0..8) sOfn(d)
for (s in sOfn(9)) bin[s]++
}
println(bin.contentToString())
}

View file

@ -0,0 +1,44 @@
enum RID, I, SAMPLE
function s_of_n(sequence env, integer item)
integer i = env[I] + 1,
n = length(env[SAMPLE])
env[I] = i
if i<=n then
env[SAMPLE][i] = item
elsif n/i>rnd() then
env[SAMPLE][rand(n)] = item
end if
return env
end function
function s_of_n_creator(int n)
return {routine_id("s_of_n"),0,repeat(0,n)}
end function
function invoke(sequence env, args)
env = call_func(env[RID],prepend(args,env))
return env
end function
function test(integer n, sequence items)
sequence env = s_of_n_creator(n)
for i=1 to length(items) do
-- env = s_of_n(env,items[i])
env = invoke(env, {items[i]})
end for
return env[SAMPLE]
end function
procedure main()
sequence items_set = tagset(9,0)
sequence frequencies = repeat(0,length(items_set))
for i=1 to 100000 do
sequence res = test(3, items_set)
for j=1 to length(res) do
frequencies[res[j]+1] += 1
end for
end for
?frequencies
end procedure
main()

View file

@ -0,0 +1,9 @@
enum RID, I, SAMPLE, CLOSURE_LEN=$
...
function s_of_n_creator(int n)
sequence closure = repeat(0,CLOSURE_LEN)
closure[RID] = routine_id("s_of_n")
closure[I] = 0
closure[SAMPLE] = repeat(0,n)
return closure
end function