This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,3 @@
(defn powers [m] (for [n (iterate inc 1)] (reduce * (repeat m n)))))
(def squares (powers 2))
(take 5 squares) ; => (1 4 9 16 25)

View file

@ -0,0 +1,11 @@
(defn squares-not-cubes
([] (squares-not-cubes (powers 2) (powers 3)))
([squares cubes]
(loop [[p2first & p2rest :as p2s] squares, [p3first & p3rest :as p3s] cubes]
(cond
(= p2first p3first) (recur p2rest p3rest)
(> p2first p3first) (recur p2s p3rest)
:else (cons p2first (lazy-seq (squares-not-cubes p2rest p3s)))))))
(->> (squares-not-cubes) (drop 20) (take 10))
; => (529 576 625 676 784 841 900 961 1024 1089)

View file

@ -0,0 +1,6 @@
(defn seq->fn [sequence]
(let [state (atom (cons nil sequence))]
(fn [] (first (swap! state rest)))
(def f (seq->fn (squares-not-cubes)))
[(f) (f) (f)] ; => [4 9 16]

View file

@ -1,61 +1,8 @@
import std.stdio, std.bigint, std.range, std.algorithm;
struct Filtered(R1, R2) if (is(ElementType!R1 == ElementType!R2)) {
R1 s1;
R2 s2;
alias ElementType!R1 T;
T current, source, filter;
this(R1 r1, R2 r2) {
s1 = r1;
s2 = r2;
source = s1.front();
filter = s2.front();
_next();
}
const bool empty = false;
@property T front() { return current; }
void popFront() { _next(); }
private void _next() {
while (true) {
if (source > filter) {
s2.popFront();
filter = s2.front();
continue;
} else if (source < filter) {
current = source;
s1.popFront();
source = s1.front();
break;
}
s1.popFront();
source = s1.front();
}
}
}
auto filtered(R1, R2)(R1 r1, R2 r2)
if (is(ElementType!R1 == ElementType!R2)) {
return Filtered!(R1, R2)(r1, r2);
}
struct Count(T) {
T n;
const bool empty = false;
@property T front() { return n; }
void popFront() { /* n++; */ n += 1; }
}
Count!T count(T)(T start) { return Count!T(start); }
Count!T count(T)() { return Count!T(cast(T)0); }
void main() {
auto squares = count!BigInt().map!q{a ^^ 2}();
auto cubes = count!BigInt().map!q{a ^^ 3}();
auto f = filtered(squares, cubes);
import std.stdio, std.bigint, std.range, std.algorithm;
popFrontN(f, 20);
writeln(take(f, 10));
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
squares.setDifference(cubes).drop(20).take(10).writeln;
}

View file

@ -1,33 +1,12 @@
import std.stdio, std.traits;
auto powers(in double e) pure nothrow {
double i = 0.0;
return () => i++ ^^ e;
}
auto filter(D)(D af, D bf) if (isCallable!D) {
double a = af(), b = bf();
return {
double r;
while (true) {
if (a < b) {
r = a;
a = af();
break;
}
if (b == a)
a = af();
b = bf();
}
return r;
};
}
void main() {
auto fgen = filter(powers(2), powers(3));
foreach (i; 0 .. 20)
fgen();
foreach (i; 0 .. 10)
write(fgen(), " ");
writeln();
import std.stdio, std.bigint, std.range, std.algorithm;
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
squares
.filter!(s => cubes.find!(c => c >= s).front != s)
.drop(20)
.take(10)
.writeln;
}

View file

@ -0,0 +1,47 @@
import std.stdio, std.bigint, std.range, std.algorithm;
struct Filtered(R1, R2) if (is(ElementType!R1 == ElementType!R2)) {
R1 s1;
R2 s2;
alias ElementType!R1 T;
T front, source, filter;
this(R1 r1, R2 r2) {
s1 = r1;
s2 = r2;
source = s1.front;
filter = s2.front;
popFront;
}
static immutable empty = false;
void popFront() {
while (true) {
if (source > filter) {
s2.popFront;
filter = s2.front;
continue;
} else if (source < filter) {
front = source;
s1.popFront;
source = s1.front;
break;
}
s1.popFront;
source = s1.front;
}
}
}
auto filtered(R1, R2)(R1 r1, R2 r2) // Helper function.
if (isInputRange!R1 && isInputRange!R2 &&
is(ElementType!R1 == ElementType!R2)) {
return Filtered!(R1, R2)(r1, r2);
}
void main() {
auto squares = 0.sequence!"n".map!(i => i.BigInt ^^ 2);
auto cubes = 0.sequence!"n".map!(i => i.BigInt ^^ 3);
filtered(squares, cubes).drop(20).take(10).writeln;
}

View file

@ -0,0 +1,34 @@
import std.stdio;
auto powers(in double e) pure nothrow {
double i = 0;
return () => i++ ^^ e;
}
auto filter2(D)(D af, D bf) {
double a = af(), b = bf();
return {
double r;
while (true) {
if (a < b) {
r = a;
a = af();
break;
}
if (b == a)
a = af();
b = bf();
}
return r;
};
}
void main() {
auto fgen = filter2(2.powers, 3.powers);
foreach (immutable i; 0 .. 20)
fgen();
foreach (immutable i; 0 .. 10)
write(fgen(), " ");
writeln;
}

View file

@ -0,0 +1,37 @@
-module( generator ).
-export( [filter/2, next/1, power/1, task/0] ).
filter( Source_pid, Remove_pid ) ->
First_remove = next( Remove_pid ),
erlang:spawn( fun() -> filter_loop(Source_pid, Remove_pid, First_remove) end ).
next( Pid ) ->
Pid ! {next, erlang:self()},
receive X -> X end.
power( M ) -> erlang:spawn( fun() -> power_loop(M, 0) end ).
task() ->
Squares_pid = power( 2 ),
Cubes_pid = power( 3 ),
Filter_pid = filter( Squares_pid, Cubes_pid ),
[next(Filter_pid) || _X <- lists:seq(1, 20)],
[next(Filter_pid) || _X <- lists:seq(1, 10)].
filter_loop( Pid1, Pid2, N2 ) ->
receive
{next, Pid} ->
{N, New_N2} = filter_loop_next( next(Pid1), N2, Pid1, Pid2 ),
Pid ! N
end,
filter_loop( Pid1, Pid2, New_N2 ).
filter_loop_next( N1, N2, Pid1, Pid2 ) when N1 > N2 -> filter_loop_next( N1, next(Pid2), Pid1, Pid2 );
filter_loop_next( N, N, Pid1, Pid2 ) -> filter_loop_next( next(Pid1), next(Pid2), Pid1, Pid2 );
filter_loop_next( N1, N2, _Pid1, _Pid2 ) -> {N1, N2}.
power_loop( M, N ) ->
receive {next, Pid} -> Pid ! erlang:round(math:pow(N, M) ) end,
power_loop( M, N + 1 ).

View file

@ -1,12 +1,10 @@
powers m = map (^ m) [0..]
import Data.List.Ordered
filtered (x:xs) (y:ys) | x > y = filtered (x:xs) ys
| x < y = x : filtered xs (y:ys)
| otherwise = filtered xs (y:ys)
powers m = map (^ m) [0..]
squares = powers 2
cubes = powers 3
f = filtered squares cubes
foo = filter (not . has cubes) squares
main :: IO ()
main = print $ take 10 $ drop 20 $ f
main = print $ take 10 $ drop 20 $ foo

View file

@ -0,0 +1,44 @@
Generate: procedure options (main); /* 27 October 2013 */
declare j fixed binary;
declare r fixed binary;
/* Ignore the first 20 values */
do j = 1 to 20;
/* put edit (filter() ) (f(6)); */
r = filter ();
end;
put skip;
do j = 1 to 10;
put edit (filter() ) (f(6));
end;
/* filters out cubes from the result of the square generator. */
filter: procedure returns (fixed binary);
declare n fixed binary static initial (-0);
declare (i, j, m) fixed binary;
do while ('1'b);
m = squares();
r = 0;
do j = 1 to m;
if m = cubes() then go to ignore;
end;
return (m);
ignore:
end;
end filter;
squares: procedure returns (fixed binary);
declare i fixed binary static initial (-0);
i = i + 1;
return (i**2);
end squares;
cubes: procedure returns (fixed binary);
r = r + 1;
return (r**3);
end cubes;
end Generate;

View file

@ -0,0 +1,25 @@
powers = function(m)
{n = -1
function()
{n <<- n + 1
n^m}}
noncubic.squares = local(
{squares = powers(2)
cubes = powers(3)
cube = cubes()
function()
{square = squares()
while (1)
{if (square > cube)
{cube <<- cubes()
next}
else if (square < cube)
{return(square)}
else
{square = squares()}}}})
for (i in 1:20)
noncubic.squares()
for (i in 1:10)
message(noncubic.squares())