4 ms·
And the same thing in D: struct Square { int sideLength; } struct Rectangle { int length; int width; } import
by he_the_great 12y ago
And the same thing in D:
struct Square
{
int sideLength;
}
struct Rectangle {
int length;
int width;
}
import std.variant;
alias Shape = Algebraic!(Square, Rectangle);
import std.typecons;
auto to_shape(Tuple!(int, int) shape) pure nothrow
{
if (shape[0] == shape[1]) return Shape(Square(shape[0]));
else return Shape(Rectangle(shape.expand));
}
void main() {
auto unknowns = [
tuple(5, 4),
tuple(3, 2),
tuple(4, 4)
];
import std.stdio;
import std.algorithm;
writeln(unknowns.map!(to_shape));
}
[Rectangle(5, 4), Rectangle(3, 2), Square(4)]
- codygman 12y agoI've been debating whether or not to choose C++ or D to learn as my "when performance really matters" language. I'm still going to learn Rust, but I'm very interested in D. I also like that it has a pure keyword ;)