Can't iterate over range
Profile Anaysis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Feb 4 06:53:14 PST 2017
On Saturday, 4 February 2017 at 14:35:37 UTC, ag0aep6g wrote:
> On 02/04/2017 12:31 PM, Profile Anaysis wrote:
>> I am trying to iterate over the combinations of a set using
>> the code
>>
>> https://rosettacode.org/wiki/Power_set#D
>>
>> I have an array which I call powerSet on and I get a result of
>> MapResult. I have tried to foreach or front/popFront and even
>> each() on
>> it but I can never get the result as the same array type that
>> I started
>> with.
>
> To create an array from a range, you can use std.array.array:
>
> http://dlang.org/phobos/std_array.html#.array
>
>> What is MapResult(the algorithm that generates the result
>> lazily?)
>
> Yes, it's a struct that gives you one element at a time, lazily
> generated.
>
>> and
>> how do we easily iterate over the results like we would with
>> an array?
>
> foreach and empty/front/popFront should both work. If you can't
> get them to work, show what you tried.
well, I simply took the code from the page I linked and did a
front() on the MapResult and it say the type was wrong. I thought
it would give me the type I put in which was an array.
I guess maybe I needed to convert it to an array though... but I
didn't try. I moved on it iterating over it manually.
someStruct[] X;
someStruct[] x = powerSet(X).popFront();
gave me an error. Was saying I was getting another MapResult,
which I thought it shouldn't but maybe I just needed to convert
it to an array.
More information about the Digitalmars-d-learn
mailing list