2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,47 @@
|
|||
defmodule Generator do
|
||||
def filter( source_pid, remove_pid ) do
|
||||
first_remove = next( remove_pid )
|
||||
spawn( fn -> filter_loop(source_pid, remove_pid, first_remove) end )
|
||||
end
|
||||
|
||||
def next( pid ) do
|
||||
send(pid, {:next, self})
|
||||
receive do
|
||||
x -> x
|
||||
end
|
||||
end
|
||||
|
||||
def power( m ), do: spawn( fn -> power_loop(m, 0) end )
|
||||
|
||||
def task do
|
||||
squares_pid = power( 2 )
|
||||
cubes_pid = power( 3 )
|
||||
filter_pid = filter( squares_pid, cubes_pid )
|
||||
for _x <- 1..20, do: next(filter_pid)
|
||||
for _x <- 1..10, do: next(filter_pid)
|
||||
end
|
||||
|
||||
defp filter_loop( pid1, pid2, n2 ) do
|
||||
receive do
|
||||
{:next, pid} ->
|
||||
{n, new_n2} = filter_loop_next( next(pid1), n2, pid1, pid2 )
|
||||
send( pid, n )
|
||||
filter_loop( pid1, pid2, new_n2 )
|
||||
end
|
||||
end
|
||||
|
||||
defp filter_loop_next( n1, n2, pid1, pid2 ) when n1 > n2, do:
|
||||
filter_loop_next( n1, next(pid2), pid1, pid2 )
|
||||
defp filter_loop_next( n, n, pid1, pid2 ), do:
|
||||
filter_loop_next( next(pid1), next(pid2), pid1, pid2 )
|
||||
defp filter_loop_next( n1, n2, _pid1, _pid2 ), do: {n1, n2}
|
||||
|
||||
defp power_loop( m, n ) do
|
||||
receive do
|
||||
{:next, pid} -> send( pid, round(:math.pow(n, m) ) )
|
||||
end
|
||||
power_loop( m, n + 1 )
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect Generator.task
|
||||
53
Task/Generator-Exponential/Java/generator-exponential.java
Normal file
53
Task/Generator-Exponential/Java/generator-exponential.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import java.util.function.LongSupplier;
|
||||
import static java.util.stream.LongStream.generate;
|
||||
|
||||
public class GeneratorExponential implements LongSupplier {
|
||||
private LongSupplier source, filter;
|
||||
private long s, f;
|
||||
|
||||
public GeneratorExponential(LongSupplier source, LongSupplier filter) {
|
||||
this.source = source;
|
||||
this.filter = filter;
|
||||
f = filter.getAsLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getAsLong() {
|
||||
s = source.getAsLong();
|
||||
|
||||
while (s == f) {
|
||||
s = source.getAsLong();
|
||||
f = filter.getAsLong();
|
||||
}
|
||||
|
||||
while (s > f) {
|
||||
f = filter.getAsLong();
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
generate(new GeneratorExponential(new SquaresGen(), new CubesGen()))
|
||||
.skip(20).limit(10)
|
||||
.forEach(n -> System.out.printf("%d ", n));
|
||||
}
|
||||
}
|
||||
|
||||
class SquaresGen implements LongSupplier {
|
||||
private long n;
|
||||
|
||||
@Override
|
||||
public long getAsLong() {
|
||||
return n * n++;
|
||||
}
|
||||
}
|
||||
|
||||
class CubesGen implements LongSupplier {
|
||||
private long n;
|
||||
|
||||
@Override
|
||||
public long getAsLong() {
|
||||
return n * n * n++;
|
||||
}
|
||||
}
|
||||
30
Task/Generator-Exponential/PHP/generator-exponential.php
Normal file
30
Task/Generator-Exponential/PHP/generator-exponential.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
function powers($m) {
|
||||
for ($n = 0; ; $n++) {
|
||||
yield pow($n, $m);
|
||||
}
|
||||
}
|
||||
|
||||
function filtered($s1, $s2) {
|
||||
while (true) {
|
||||
list($v, $f) = [$s1->current(), $s2->current()];
|
||||
if ($v > $f) {
|
||||
$s2->next();
|
||||
continue;
|
||||
} else if ($v < $f) {
|
||||
yield $v;
|
||||
}
|
||||
$s1->next();
|
||||
}
|
||||
}
|
||||
|
||||
list($squares, $cubes) = [powers(2), powers(3)];
|
||||
$f = filtered($squares, $cubes);
|
||||
foreach (range(0, 19) as $i) {
|
||||
$f->next();
|
||||
}
|
||||
foreach (range(20, 29) as $i) {
|
||||
echo $i, "\t", $f->current(), "\n";
|
||||
$f->next();
|
||||
}
|
||||
?>
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
sub powers($m) { 0..* X** $m }
|
||||
sub powers($m) { $m XR** 0..* }
|
||||
|
||||
my @squares = powers(2);
|
||||
my @cubes = powers(3);
|
||||
|
||||
sub infix:<without> (@orig,@veto) {
|
||||
sub infix:<with-out> (@orig,@veto) {
|
||||
gather for @veto -> $veto {
|
||||
take @orig.shift while @orig[0] before $veto;
|
||||
@orig.shift if @orig[0] eqv $veto;
|
||||
}
|
||||
}
|
||||
|
||||
say (@squares without @cubes)[20 ..^ 20+10].join(', ');
|
||||
say (@squares with-out @cubes)[20 ..^ 20+10].join(', ');
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@
|
|||
|
||||
def powers(m)
|
||||
return enum_for(__method__, m) unless block_given?
|
||||
|
||||
n = 0
|
||||
loop { yield n ** m; n += 1 }
|
||||
0.step{|n| yield n**m}
|
||||
end
|
||||
|
||||
def squares_without_cubes
|
||||
|
|
|
|||
|
|
@ -2,17 +2,15 @@
|
|||
|
||||
def powers(m)
|
||||
return enum_for(__method__, m) unless block_given?
|
||||
|
||||
n = 0
|
||||
loop { yield n ** m; n += 1 }
|
||||
0.step{|n| yield n**m}
|
||||
end
|
||||
|
||||
def squares_without_cubes
|
||||
return enum_for(__method__) unless block_given?
|
||||
|
||||
cubes = powers(3)
|
||||
cubes = powers(3) #no block, so this is the first generator
|
||||
c = cubes.next
|
||||
squares = powers(2)
|
||||
squares = powers(2) # second generator
|
||||
loop do
|
||||
s = squares.next
|
||||
c = cubes.next while c < s
|
||||
|
|
@ -20,6 +18,6 @@ def squares_without_cubes
|
|||
end
|
||||
end
|
||||
|
||||
answer = squares_without_cubes
|
||||
answer = squares_without_cubes # third generator
|
||||
20.times { answer.next }
|
||||
p 10.times.map { answer.next }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
f = { |m| {:x, x<-(0..) } ** m };
|
||||
g = f.(2);
|
||||
g.nextN(10); // answers [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(
|
||||
f = Pseries(0, 1)
|
||||
g = f ** 2;
|
||||
g.asStream.nextN(10); // answers [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
|
||||
)
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
(
|
||||
var filter = { |a, b, func| // both streams are assumed to be ordered
|
||||
Prout {
|
||||
var astr, bstr;
|
||||
var aval, bval;
|
||||
astr = a.asStream;
|
||||
bstr = b.asStream;
|
||||
bval = bstr.next;
|
||||
while {
|
||||
aval = astr.next;
|
||||
aval.notNil
|
||||
} {
|
||||
while {
|
||||
bval.notNil and: { bval < aval }
|
||||
} {
|
||||
bval = bstr.next;
|
||||
};
|
||||
if(func.value(aval, bval)) { aval.yield };
|
||||
}
|
||||
}
|
||||
};
|
||||
var without = filter.(_, _, { |a, b| a != b }); // partially apply function
|
||||
|
||||
f = Pseries(0, 1);
|
||||
|
||||
g = without.(f ** 2, f ** 3);
|
||||
h = g.drop(20);
|
||||
h.asStream.nextN(10);
|
||||
)
|
||||
|
||||
answers: [ 529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089 ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue