Print a triangle
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 29 11:45:39 PDT 2016
On Fri, Apr 29, 2016 at 12:01:19PM +0000, Joel via Digitalmars-d-learn wrote:
> On Friday, 29 April 2016 at 11:31:51 UTC, rikki cattermole wrote:
> >Not entirely the goal I'm guessing output wise, but this works.
> >
> >import std.range : repeat;
> >foreach(line; 1 .. 11) {
> > writeln('#'.repeat(line));
> >}
>
> That is shorter than my foreach version, but I want one that doesn't
> use foreach in it at all.
Try this:
auto triangle(int i) {
import std.algorithm.iteration : map, joiner;
import std.range : iota, repeat;
import std.utf : byCodeUnit; // this is a hack
return iota(i).map!(i => '#'.repeat(i))
.joiner("\n".byCodeUnit);
}
void main() {
import std.stdio : writeln;
writeln(triangle(10));
}
Unfortunately, due to some silly autodecoding issues in Phobos,
byCodeUnit is a necessary hack to make this work. I'll file a bug for
this.
T
--
Today's society is one of specialization: as you grow, you learn more and more about less and less. Eventually, you know everything about nothing.
More information about the Digitalmars-d-learn
mailing list