Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 29, 2016 Print a triangle | ||||
---|---|---|---|---|
| ||||
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); |
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel | 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));
}
|
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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.
|
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel | 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)))
|
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel | 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. |
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
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 |
April 29, 2016 Re: Print a triangle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Coulombe |
[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.
|
Copyright © 1999-2021 by the D Language Foundation