Iterable in Dart

Iterable in Dart#

Overview of the Iterable Interface#

In Dart, the Iterable interface represents a collection of elements that can be accessed sequentially. It forms the foundation for various collection types like List and Set. The main feature of an Iterable is that it allows stepping through its elements using an iterator, making it essential for operations like looping or mapping over collections.

  • Iterable is a collection of values that you can iterate over, meaning you can move from one value to the next in sequence.
  • Common types of iterable collections include lists, sets, and custom iterables.

Common Iterable Methods#

About#

The Iterable interface provides several methods to work with collections efficiently. Some common methods include:

  • map(): Transforms elements in the iterable.
  • reduce(): Combines elements into a single value.
  • fold(): Accumulates values based on an initial value and a function.
  • any(): Checks if any element satisfies a condition.
  • every(): Checks if all elements satisfy a condition.
  • toList(): Converts an iterable into a list.
  • toSet(): Converts an iterable into a set.

How It Works#

The Iterable interface works by exposing an iterator over the collection. This allows elements to be accessed one by one in sequence. The iterable can be lazily evaluated, meaning it only computes elements as needed, which is memory efficient for large or infinite collections.

For example, methods like map and where create a new iterable that is only evaluated when its elements are accessed (e.g., by calling toList() or forEach()).

How to Use#

The Iterable interface and its methods can be used in various Dart collections such as lists, sets, or even custom iterables. You can call these methods directly on collections to perform operations like transformation, filtering, or reduction.

Example#

Example 1: Using map() to Transform a List#

void main() {
  List<int> numbers = [1, 2, 3, 4];
  Iterable<int> doubledNumbers = numbers.map((num) => num * 2);

  print(doubledNumbers.toList()); // Output: [2, 4, 6, 8]
}

In this example, the map() method is used to create a new iterable where each number is doubled.

Example 2: Using reduce() to Sum a List of Numbers#

void main() {
  List<int> numbers = [1, 2, 3, 4];
  int sum = numbers.reduce((a, b) => a + b);

  print(sum); // Output: 10
}

The reduce() method combines all the elements of the list into a single sum.

Example 3: Checking Conditions with any() and every()#

void main() {
  List<int> numbers = [1, 2, 3, 4];

  bool hasEven = numbers.any((num) => num.isEven);
  bool allEven = numbers.every((num) => num.isEven);

  print(hasEven); // Output: true
  print(allEven); // Output: false
}

In this example, any() checks if there’s any even number, while every() checks if all numbers are even.

Conclusion#

The Iterable interface in Dart is a powerful tool for working with collections. It provides numerous methods to manipulate, filter, and transform data efficiently. Using methods like map(), reduce(), and fold(), developers can handle collections in a clean, functional style.