www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Print a triangle

reply Joel <joelcnz gmail.com> writes:
What is a quick way to print a triangle? I'm thinking without 
foreach, not like I have here.

foreach(line; iota(1, 10 + 1)) {
	writeln("#".replicate(line));
}

These don't work:

iota(1, 10 + 1).
tee!((a) => { writeln("#".replicate(a)); });

string result;
iota(1, 10 + 1).
tee!((a) => result ~= "#".replicate(a) ~ "\n");
writeln(result);
Apr 29 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 29/04/2016 11:23 PM, Joel wrote:
 What is a quick way to print a triangle? I'm thinking without foreach,
 not like I have here.

 foreach(line; iota(1, 10 + 1)) {
     writeln("#".replicate(line));
 }

 These don't work:

 iota(1, 10 + 1).
 tee!((a) => { writeln("#".replicate(a)); });

 string result;
 iota(1, 10 + 1).
 tee!((a) => result ~= "#".replicate(a) ~ "\n");
 writeln(result);
Not entirely the goal I'm guessing output wise, but this works. import std.range : repeat; foreach(line; 1 .. 11) { writeln('#'.repeat(line)); }
Apr 29 2016
parent reply Joel <joelcnz gmail.com> writes:
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.
Apr 29 2016
next sibling parent reply Michael Coulombe <kirsybuu gmail.com> writes:
On Friday, 29 April 2016 at 12:01:19 UTC, Joel 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: iota(1,11).each!(a => writeln("#".replicate(a)))
Apr 29 2016
parent Joel <joelcnz gmail.com> writes:
[snip]

On Friday, 29 April 2016 at 13:20:47 UTC, Michael Coulombe wrote:
 Try this:

 iota(1,11).each!(a => writeln("#".replicate(a)))
Yes, this is what I was looking for! It's my birthday today.
Apr 29 2016
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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.
Apr 29 2016
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, Apr 29, 2016 at 11:45:39AM -0700, H. S. Teoh via Digitalmars-d-learn
wrote:
[...]
 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.
[...] https://issues.dlang.org/show_bug.cgi?id=15972 T -- Skill without imagination is craftsmanship and gives us many useful objects such as wickerwork picnic baskets. Imagination without skill gives us modern art. -- Tom Stoppard
Apr 29 2016