6 ms·
> I think I'd miss patterns like `if (arr.length) { ... }` At least for this particular example, dart supports an 'isNotEmpty' property on all iterables[0], so
by oblique63 12y ago
> I think I'd miss patterns like `if (arr.length) { ... }`
At least for this particular example, dart supports an 'isNotEmpty' property on all iterables[0], so it'd just be this:
if (arr.isNotEmpty) { ... }
The core libraries support a lot of useful properties like that.
[0] https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-core.Iterable#id_isNotEmpty https://api.dartlang.org/apidocs/channels/stable/dartdoc-vie...
- munificent 12y agoIt's actually important to prefer .isNotEmpty over .length here. In some iterables (think generators, or sequences transformed by higher-order functions), calculating the length requires traversing the entire sequence where .isNotEmpty can stop after finding a single item.