Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,9 @@
A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided. Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”. Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
{{omit from|Lilypond}}
[[Category:Non parametric generators]]
[[Category:Stateful transactions]]
A generator is an executable entity (like a function or procedure) that contains code that yields a sequence of values, one at a time, so that each time you call the generator, the next value in the sequence is provided.
Generators are often built on top of coroutines or objects so that the internal state of the object is handled “naturally”.
Generators are often used in situations where a sequence is potentially infinite, and where it is possible to construct the next value of the sequence with only minimal state.
'''Task description'''

View file

@ -0,0 +1,28 @@
import std.stdio, std.range, std.algorithm, std.concurrency, std.bigint;
auto powers(in uint m) pure nothrow @safe {
return 0.sequence!"n".map!(i => i.BigInt ^^ m);
}
auto filtered(R1, R2)(R1 r1, R2 r2) /*@safe*/
if (isForwardRange!R1 && isForwardRange!R2 &&
is(ElementType!R1 == ElementType!R2)) {
return new Generator!(ElementType!R1)({
auto v = r1.front; r1.popFront;
auto f = r2.front; r2.popFront;
while (true) {
if (v > f) {
f = r2.front; r2.popFront;
continue;
} else if (v < f)
yield(v);
v = r1.front; r1.popFront;
}
});
}
void main() {
auto squares = 2.powers, cubes = 3.powers;
filtered(squares, cubes).drop(20).take(10).writeln;
}

View file

@ -0,0 +1,16 @@
drop(n, gen) = (for i=1:n gen() end ; gen)
take(n, gen) = [gen() for i=1:n]
pgen(n) = let x=0; () -> (x+=1)^n; end
function genfilter(gen1, gen2)
let r1 = -Inf, r2 = gen2()
() -> begin
r1 = gen1()
while r2 < r1 r2 = gen2() end
while r1 == r2 r1 = gen1() end
r1
end
end
end

View file

@ -1,21 +1,22 @@
# This solution cheats and uses only one generator!
def powers(m)
return enum_for(:powers, m) unless block_given?
return enum_for(__method__, m) unless block_given?
n = 0
loop { yield n ** m; n += 1 }
end
def squares_without_cubes
return enum_for(:squares_without_cubes) unless block_given?
return enum_for(__method__) unless block_given?
cubes = powers(3)
c = cubes.next
powers(2) do |s|
(c = cubes.next) until c >= s
c = cubes.next while c < s
yield s unless c == s
end
end
p squares_without_cubes.take(30).drop(20)
# p squares_without_cubes.lazy.drop(20).first(10) # Ruby 2.0+

View file

@ -1,25 +1,25 @@
# This solution uses three generators.
def powers(m)
return enum_for(:powers, m) unless block_given?
return enum_for(__method__, m) unless block_given?
n = 0
loop { yield n ** m; n += 1 }
end
def squares_without_cubes
return enum_for(:squares_without_cubes) unless block_given?
return enum_for(__method__) unless block_given?
cubes = powers(3)
c = cubes.next
squares = powers(2)
loop do
s = squares.next
(c = cubes.next) until c >= s
c = cubes.next while c < s
yield s unless c == s
end
end
answer = squares_without_cubes
20.times { answer.next }
p (1..10).map { answer.next }
p 10.times.map { answer.next }

View file

@ -0,0 +1,14 @@
def filtered(s1, s2)
return enum_for(__method__, s1, s2) unless block_given?
v, f = s1.next, s2.next
loop do
v > f and f = s2.next and next
v < f and yield v
v = s1.next
end
end
squares, cubes = powers(2), powers(3)
f = filtered(squares, cubes)
p f.take(30).last(10)
# p f.lazy.drop(20).first(10) # Ruby 2.0+

View file

@ -0,0 +1,27 @@
(define (power-seq n)
(let ((i 0))
(lambda ()
(set! i (+ 1 i))
(expt i n))))
(define (filter-seq m n)
(let* ((s1 (power-seq m)) (s2 (power-seq n))
(a 0) (b 0))
(lambda ()
(set! a (s1))
(let loop ()
(if (>= a b) (begin
(cond ((> a b) (set! b (s2)))
((= a b) (set! a (s1))))
(loop))))
a)))
(let loop ((seq (filter-seq 2 3)) (i 0))
(if (< i 30)
(begin
(if (> i 20)
(begin
(display (seq))
(newline))
(seq))
(loop seq (+ 1 i)))))