[Issue 4761] New: std.array.mul()
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Aug 29 15:31:33 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4761
Summary: std.array.mul()
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Keywords: patch
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-08-29 15:31:21 PDT ---
In Python I use very often the * (__mul__) list operator, it's handy in many
situations (it is not lazy):
>>> "abc" * -1
''
>>> "abc" * 0
''
>>> "abc" * 1
'abc'
>>> "abc" * 5
'abcabcabcabcabc'
>>> [0] * 5
[0, 0, 0, 0, 0]
It's not a good idea to add a similar product operator to D arrays because
despite vector mult operation will probably require [] (while the mul doesn't
require it), the two are too much syntactically close, they risk being too much
bug-prone.
So I suggest to simply add this mul() to std.array (this must be eager, not a
lazy range, otherwise it's not handy for its purposes):
/// ...
T[] mul(T)(T[] items, int times) {
T[] result;
static if(!is( typeof(items) == void[] )) {
if (times > 0 && items.length > 0) {
if (items.length == 1) {
result.length = times;
result[] = items[0];
} else {
result.length = items.length * times;
for (size_t pos; pos < result.length; pos += items.length)
result[pos .. pos+items.length] = items;
}
}
}
return result;
}
// demo ----------
import std.stdio;
void main() {
auto a1 = [0].mul(10);
writeln(a1);
auto a2 = [0, 5, 10].mul(4);
writeln(a2);
}
More unittests and a ddoct on request.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list