How to resolve the algorithm Generator/Exponential step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Generator/Exponential step by step in the D programming language
Table of Contents
Problem Statement
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.
Note that this task requires the use of generators in the calculation of the result.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Generator/Exponential step by step in the D programming language
Source code in the d programming language
void main() {
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.setDifference(cubes).drop(20).take(10).writeln;
}
void main() {
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;
}
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;
}
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;
}
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;
}
You may also check:How to resolve the algorithm Align columns step by step in the sed programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Elena programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Nim programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the MAXScript programming language