Using algorithms with ranges
Brett
Brett at gmail.com
Thu Oct 3 04:33:10 UTC 2019
I routinely have to generate data using points sequentially and
refer to previous points in the data set, maybe even search them.
I also may have to break up the algorithm in to parts.
I'd like to get more in to ranges but I simply do not use them
much because I don't know all the fancy stuff that makes them
more useful over just doing it manually(usually for loop or 3).
Is there a way to express an algorithm that generates a data
point that may depend on previous data points and "rangify" them?
One usually starts with some integer range: iota(-5,5,1)
The idea then is to be able to generates points off the range but
have access to the previous generated points without having to
regenerate them(very inefficient).
so if P_k is our kth point then we need to have access to P_j for
j < k.
The algorithm may "branch"(multiple sub algorithms). For example,
maybe for negatives we have a different algorithm.
The idea is to do it as efficiently and simple as possible. If it
doesn't beat for loops and adds complexity then it is really not
better than using loops.
I would think of something like
iota(-5,5,1).kmap!((k,History)=>{return History[k-1]+1;})
But the problems is that what if we want to have a different
algorithm for the negatives? Obviously we could add an if
statement but then that is executed for every k. We could split
iota but we then break History up and that causes problems when
we really want a contiguous History. Then there is the issue of
accessing invalid history values which then requires checks and
fixes.
For example, the above algorithm works fine but only has an issue
when k = 0. The entire rest of the algorithm suffers because of
this issue. There are ways around it such as being able to
provide "out of bounds" values such as having History[k] = 0 when
k < 0.
These are the types of differences between
for(int k = 0; ....)
if (k < 1) { return 0; }
else return History[k-1]+1;
and
if (k == 0) return 0;
for(int k = 1; ....)
return History[k-1]+1;
One could maybe have something like
iota(-5,5,1).kmap!((-5,History)=>{return
0;}).kmap!((k>-5,History)=>{return History[k-1]+1;})
but of course doesn't make a lot of syntactical sense... yet it
could be done with the appropriate machinery. Of course these are
simple examples.
The idea here is that I would like to generate everything range
like without too much work.
Is it possible in D?
Again, if the range code is very complicated and one has to jump
through a lot of hoops then it is easier to just do it manually.
The main thing I'm interested in is the "History" feature and
maybe a way to easily partition the range(range of ranges).
More information about the Digitalmars-d-learn
mailing list