Sum string lengths
MoonlightSentinel
moonlightsentinel at disroot.org
Wed May 13 14:04:48 UTC 2020
On Wednesday, 13 May 2020 at 13:52:13 UTC, Andrey wrote:
> Hi,
> I want to sum lengths of all strings in array:
>> auto data = ["qwerty", "az", "!!!!"];
>
> Fold and reduce doesn't work:
>> auto result = data.fold!`a + b.length`(0U);
>
> gives error:
>> static assert: "Incompatible function/seed/element:
>> binaryFun/uint/string"
>
> How to do it in one line?
The problem is that you provide a uint as seed (which determines
the type of the accumulated value) but your function returns a
ulong (or rather size_t).
You can fix this by changing the type of your seed, either use
ulong or size_t.
auto result = data.fold!`a + b.length`(0UL);
auto result = data.fold!`a + b.length`(size_t(0));
More information about the Digitalmars-d-learn
mailing list