repeat

Gareth Charnock gareth.charnock at gmail.com
Mon Jan 17 16:48:47 PST 2011


On 17/01/11 18:10, Andrei Alexandrescu wrote:
> 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.
>
>
> Andrei

Perhaps...

repeat("abc",3) -> ["abc","abc","abc"]
repeatFlatten("abc",3) -> "abcabcabc" or perhaps repeatFlat("abc",3)

?



More information about the Digitalmars-d mailing list