This consists of two problems, a very standard one (flattening a list) and then joining the strings adequately.
flatten(Iterable l) => l.fold([], (List list, element) {
if (element is Iterable)
list.addAll(flatten(element));
else
list.add(element);
return list;
});
concat(Iterable l) => l.fold([], (List list, element) {
if (element is String && !list.isEmpty && list.last is String)
list.add(list.removeLast() + element);
else
list.add(element);
return list;
});
void main() {
var nested = [ 'a', 'w', ['e', ['f', new Object(), 'f'], 'g'], 't', 'e'];
print(concat(flatten(nested));
}
Update:
An alterantive concat
, inspired by Greg Lowe (doing exactly the same like my "large" and his one, but even more condense):
concat(Iterable list) => list.fold([], (List xs, x) => xs..add(
x is String && !xs.isEmpty && xs.last is String ? xs.removeLast() + x : x));
You can combine his and my functions as you want, they're doing exactly the same thing.