digitalmars.D.learn - Sum string lengths
- Andrey (5/9) May 13 2020 Fold and reduce doesn't work:
- H. S. Teoh (5/8) May 13 2020 data.map!(s => s.length).sum;
- MoonlightSentinel (8/17) May 13 2020 The problem is that you provide a uint as seed (which determines
- Andrey (1/1) May 14 2020 Thanks everyone.
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?
May 13 2020
On Wed, May 13, 2020 at 01:52:13PM +0000, Andrey via Digitalmars-d-learn wrote:Hi, I want to sum lengths of all strings in array:data.map!(s => s.length).sum; T -- It only takes one twig to burn down a forest.auto data = ["qwerty", "az", "!!!!"];
May 13 2020
On Wednesday, 13 May 2020 at 13:52:13 UTC, Andrey wrote:Hi, I want to sum lengths of all strings in array: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));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?
May 13 2020