repeat

Tomek Sowiński just at ask.me
Mon Jan 17 12:53:41 PST 2011


Andrei Alexandrescu napisał:

> On 1/17/11 2:14 PM, Tomek Sowiński wrote:
> > Andrei Alexandrescu napisał:
> >
> >> On 1/17/11 1:53 PM, Tomek Sowiński wrote:
> >>> Andrei Alexandrescu napisał:
> >>>
> >>>> std.range has a function repeat that repeats one value forever. For
> >>>> example, repeat(42) is an infinite range containing 42, 42, 42,...
> >>>>
> >>>> The same module also has a function replicate that repeats one value a
> >>>> specific number of times. In fact, replicate can be expressed as an
> >>>> overload of repeat, so that's what I just did (not committed yet):
> >>>> repeat(42, 100) repeats 42 one hundred times, repeat(42) repeats 42
> >>>> forever. I'll put replicate on the deprecation chute.
> >>>>
> >>>> So far so good. Now, string has its own repeat. repeat("abc", 2) returns
> >>>> the string "abcabc".
> >>>>
> >>>> I want to generalize the functionality in string's repeat and move it
> >>>> outside std.string. There is an obvious semantic clash here. If you say
> >>>> repeat("abc", 3) did you mean one string "abcabcabc" or three strings
> >>>> "abc", "abc", and "abc"?
> >>>>
> >>>> So we need distinct names for the functions. One repeats one value, the
> >>>> other repeats a range. Moreover, I'm thinking sometimes you want to
> >>>> repeat a range lazily, i.e. instead of producing "abcabc" just return a
> >>>> range that looks like it.
> >>>>
> >>>> Ideas for a good naming scheme are welcome.
> >>>
> >>> Overload cycle and call it a day?
> >>
> >> cycle(r, n) already has a meaning: cycle r for a maximum total of n
> >> elements.
> >
> > Now I'm confused. The docs say it's an initial index...
> 
> Sorry, my bad. You're right. Still, cycle(r, n) has a meaning distinct 
> from what we might need.

I don't think the initial index really useful (even the authors confirm by not bothering to unittest it:-)) My idea is to dump it in favor of popFrontN (provide a method on Cycle, let the stand-alone popFrontN statically recognize that). Bounding an infinite range is much more frequent.

Or, if you're really not keen on the idea, introduce cycleN.

> Essentially I'm looking for a name for the 
> function array(take(cycle(range), n * range.length)). That's what 
> std.string.repeat does currently.

With the above cycleN(range, n * range.length).array() doesn't look that bad. What are the use-cases that you want a separate name?

-- 
Tomek



More information about the Digitalmars-d mailing list