
Dart vs. Swift: A Comparison - markdog12
https://medium.com/coding-with-flutter/dart-vs-swift-a-comparison-6491e945dc17
======
Someone
Nice overview. My eye caught one bug: the code for _”iterates over a list of
items, using a closure to print the index and contents of each item”_ is buggy
in both examples; it will print incorrect index values if the list contains
duplicates.

It also is inefficient, searching the list for every value in the list,
turning an _O(n)_ algorithm in _O(n²)_.

It’s easy to prevent that by just keeping track of the index value, increasing
it in the lambda. That”s ugly, but both faster and correct.

In Swift, you can do better by using _enumerated()_
([https://developer.apple.com/documentation/swift/array/168783...](https://developer.apple.com/documentation/swift/array/1687832-enumerated)).
Chances are Dart has a similar construct.

(Yes, I know that’s not what is being discussed in that section, but people
will copy-paste that code and learn incorrect patterns from it. To prevent
introducing two concepts in one example, I would change the example to just
print the items, and add an item on the use of _enumerated_ )

