Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Step through range in D

Tags:

iteration

range

d

Is there a way to create a step in D ranges? For example, in python, range(1, 10, 2) gives me

        [1, 3, 5, 7, 9]

all odds within 1 .. 10

Is there a way to do this in D using foreach?

    foreach(x; 1 .. 10) {
 }

I know I can use iota(start, end, step), but I also want to add an int to the very beginning and I don't know how to convert type Result to an int.

like image 523
Phil Kurtis Avatar asked May 04 '26 14:05

Phil Kurtis


1 Answers

chain([2],iota(3,16,2));

chain concatenates ranges lazily

or you can go the other way around with filter!q{a==2||a&1}(iota(2,16));

like image 125
ratchet freak Avatar answered May 07 '26 06:05

ratchet freak