fxSumStrings

sum for strings — concatenation in linear time, not the quadratic fold.

String fxSumStrings(Iterable<String> iterable) String Fx.join([String separator = '']) // chain equivalent (no sumStrings member) Future<String> FxAsync.join([String sep = ',']) // async: no twin — pass '' explicitly

Lecture

fxSumStrings(xs) concatenates every string in the iterable, with nothing in between. Empty input returns '', the identity for concatenation, just as sum of nothing is 0. It takes a single argument, so it drops straight into a pipe.

The obvious way to write it is fold('', (acc, s) => acc + s, xs) — and that is what fxdart once did. But Dart strings are immutable: every + copies the whole accumulator into a new string, so the fold's cost grows with the square of the output length. fxSumStrings is Iterable.join(), which writes each piece once. The changelog's measurement, 20,000 strings of 10 characters: 74,620 µs → 343 µs.

There is no chain member and no async twin. On a sync chain, fx(xs).join() is the same call — Fx.join is Dart's Iterable.join, whose default separator is ''. On FxAsync, join defaults to ',' (the FxTS default), so spell the empty separator out: .join('').

Demo 1 · Basics, and the fold it replaces

Demo 2 · On a chain, sync and async

Try it yourself

Exercise: replace the quadratic fold with one call.

Related: join — the same, with a separator · sum — the numeric version · fold — why not to concatenate with it